大家好,我在下面用球衣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();
}
所以最后我在一个像::
这样的邮递员中说出一个获取请求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
所以我制作一个道具文件
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
现在我想抓住那个安静的网址并通过道具文件传递它并根据方法和网址进行相应的路由
谢谢 标记
答案 0 :(得分:0)
您可以在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
进行测试是不够的。
答案 1 :(得分:0)
当使用标准Web描述符(url-pattern / *)和带注释的端点(@Path / library)时,在这一点上可以为动态(在运行时)路由做很多事情。如果你想要或需要一些动态路线,一个选项就是把#34; app&#34;进入一个新的环境,如:
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
并添加一些标准Servlet
以转发到您想要的终点:
<servlet>
<servlet-name>route</servlet-name>
<servlet-class>my.package.Route</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>route</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
除了/ app / *请求之外,my.package.Route会被点击,您可以执行 路由以在任何地方转发请求。