One question about this code: Is it correct to use @WebServlet with @Path ?

 @WebServlet(urlPatterns = "/login",name = "LoggerServlet") public class Login extends HttpServlet { private static final long serialVersionUID = -5763766570953298418L; @Context HttpServletRequest request; @Context HttpServletResponse response; @Path("/logger/{id}") @POST @Consumes({MediaType.APPLICATION_JSON}) public void login (User user, @PathParam("id")long id) throws ConnectionFailed, CouponSystemException, ServletException,InvalidLogin,IOException, UserNotFound { LoginAuthentication validator = new LoginAuthentication(); Response resp = validator.validate(user); int status = resp.getStatus(); if(status == 200){ service(request, response); } } 

    1 answer 1

    Frankly, no. The Path Annotation is part of JAX-RS , while the WebServlet is actually servlets. The difference is that to work with the servlet, it is necessary to inherit HttpServlet and override methods doGet(httpRequest, httpResponse) , doPost(...) , those that are needed. If the HTTP method is not trusted, an error from the discharge 'server does not support this http method' will be returned.

    Meanwhile, JAX-RS is rest web services. For the rest of the web service in javaEE, you need one and only one descendant of javax.ws.rs.core.Application with the filled ApplicationPath annotation and for each service a class with the Path annotation that may look like this:

     @Path("/users") public class UsersEndpoint { @Inject private List<User> users; @GET @Path("/{id}") @Produces("application/json") public User getUser(@PathParam("id") long id) { return users.get(id); } } 

    JAX-RS is a technology of a higher level of abstraction than servlets. Often it is enough, but sometimes it is necessary to use servlets.