大家好,我有下面的示例 Restful 服务, Jersey 2
模型类
@XmlRootElement(name = "book")
@XmlType(propOrder = { "id", "name", "author", "price" })
public class Book {
private String id;
private String name;
private String price;
private String author;
public Book() {
}
public Book(String id, String name, String price, String author) {
super();
this.id = id;
this.name = name;
this.price = price;
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
我的 DAOImpl
public class BooksImpl{
static Connection conn = null;
static Statement stmt;
ResultSet rs;
public List<Book> getAllBooks() throws SQLException, ClassNotFoundException, FileNotFoundException {
getConnection();
List<Book> arrBook = new ArrayList<Book>();
rs = stmt.executeQuery(GET_ALL_BOOKS);
while (rs.next()) {
arrBook.add(new Book(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
}
rs.close();
stmt.close();
conn.close();
return arrBook;
}
服务
@Path("/library")
@Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })
@Consumes({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })
public class BookServiceImpl implements BookService {
private BooksImpl booksImpl = new BooksImpl();
@GET
public Response getBooks(@QueryParam("format") String format) throws SQLException, ClassNotFoundException, FileNotFoundException {
return Response.status(Status.OK).entity(new GenericEntity<List<Book>>(booksImpl.getAllBooks()) {
}).header(HttpHeaders.CONTENT_TYPE, "XML".equalsIgnoreCase(format)
? MediaType.APPLICATION_XML + ";charset=UTF-8" : MediaType.APPLICATION_JSON + ";charset=UTF-8")
.status(Status.OK).build();
}
所以最后我在 postman 中发出一个获取请求,比如::
http://localhost:8080/BooksJAXRS/library
这很好,我可以通过从 DB 获取所有书籍来得到回复。
我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>BooksJAXRS</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.library.books</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我的要求是对于不同的 http 方法,我们有不同的 url 端点路径,例如
GET : http://localhost:8080/BooksJAXRS/library
POST : http://localhost:8080/BooksJAXRS/library
DELETE : http://localhost:8080/BooksJAXRS/library/1
PUT : http://localhost:8080/BooksJAXRS/library/1
所以我制作了一个 Prop 文件
GET=http://localhost:8080/BooksJAXRS/library
POST=http://localhost:8080/BooksJAXRS/library
DELETE= http://localhost:8080/BooksJAXRS/library/1
PUT= http://localhost:8080/BooksJAXRS/library/1
现在我想捕获该静态 url 并将其传递到 props 文件,并根据方法和 url 相应地路由它
谢谢 标记
最佳答案
您可以在调用相同私有(private)方法的 2 个不同方法上创建 2 个映射,如下所示:
@Path("/")
...
public class BookServiceImpl implements BookService {
@Path("/")
@GET
public Response getBooksByRoot(@QueryParam("format") String format) {
return getBooks(format);
}
@Path("/library")
@GET
public Response getBooksByLibrary(@QueryParam("format") String format) {
return getBooks(format);
}
private Response getBooks(String format) {
...
}
@Path("/library")
@POST
public Response createBook(@QueryParam("name") String name) {
...
}
响应更新:
您似乎想要动态映射,因此需要使用带有正则表达式的Path
。
@Path("/")
...
public class BookServiceImpl implements BookService {
@Path("/{parameter:.*}")
@GET
public Response getBooks(@PathParam("parameter") String parameter) {
if (properties.getProperty("GET").endsWith(parameter) {
// code to get the books here
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
警告:上面的代码片段仅用于展示想法,并不意味着完美。例如,根据您的上下文,使用 endsWith
进行测试可能还不够。
关于java - 通过属性文件的 Restful url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234126/