publicJerseyResourceConfig() { /* * Servlet Filter that exposes the request to the current thread, through both org.springframework.context.i18n.LocaleContextHolder and RequestContextHolder. To be registered as filter in web.xml.<br/> Alternatively, Spring's org.springframework.web.context.request.RequestContextListener and Spring's org.springframework.web.servlet.DispatcherServlet also expose the same request context to the current thread.<br/> This filter is mainly for use with third-party servlets, e.g. the JSF FacesServlet. Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.<br/> */ register(RequestContextFilter.class);
/** * http://localhost:8080/rest/province/city/1 * * @param id * @return */ @Path("city/{id}") @GET @Produces(MediaType.APPLICATION_JSON)//表示输出json @Consumes(MediaType.APPLICATION_JSON)//表示输入为json public Result<Boolean> getCity(@PathParam("id") Long id) {
returnnewResult<>(); }
@PUT
http请求方式里面的get请求,标记这个请求方式为put
一般用来创建一个资源
1 2 3 4 5 6 7 8
@Path("city/{id}") @PUT @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result<Boolean> putCity(@PathParam("id") Long id) {
returnnewResult<>(); }
@POST
http请求方式里面的get请求,标记这个请求方式为post
一般用来创建和更新
1 2 3 4 5 6 7 8
@Path("city/{id}") @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result<Boolean> postCity(@PathParam("id") Long id) {
returnnewResult<>(); }
@DELETE
http请求方式里面的get请求,标记这个请求方式为delete
一般用来删除一个资源
1 2 3 4 5 6 7
@Path("city/{id}") @DELETE @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result<Boolean> deleteCity(@PathParam("id") Long id) { returnnewResult<>(); }
@PathParam
一般url里面的参数用PathParam
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * http://localhost:8080/rest/province/city/1 * * @param id * @return */ @Path("city/{id}") @GET @Produces(MediaType.APPLICATION_JSON)//表示输出json @Consumes(MediaType.APPLICATION_JSON)//表示输入为json public Result<Boolean> getCity(@PathParam("id") Long id) {
returnnewResult<>(); }
@QueryParam
可以输出url后面的参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * http://localhost:8080/rest/queryCity/1?name=wh * * @param id * @param name * @return */ @Path("queryCity/{id}") @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result<Boolean> queryCity(@PathParam("id") Long id, @QueryParam("name") String name) { System.out.println(name); returnnewResult<>(); }
@HeaderParam
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * http://localhost:8080/rest/queryCity/1?name=wh * * @param authKey * @param id * @param name * @return */ @Path("queryCity/{id}") @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result<Boolean> queryHeaderCity(@HeaderParam("auth") String authKey, @PathParam("id") Long id, @QueryParam("name") String name) { System.out.println(name); System.out.println(authKey); returnnewResult<>(); }