1

I'm trying to write a REST servlet that returns a list at '/connections' and an item at '/connections/{id}', but GET requests only ever go to the first doGet() function. I can't figure out how to write the web.xml to handle the id field.

What is it that I'm doing wrong in this code?

@Path("connections")
public class Connections extends HttpServlet {

    public Connections() {
        super();
    }

    @GET
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //return list
    }

    @GET
    @Path("{id}")
    protected void doGet(HttpServletRequest request, HttpServletResponse response, @PathParam("id") String id)
            throws ServletException, IOException {
        //return item from list
    }
}

and the web.xml

<web-app>
    <display-name>test</display-name>
    <servlet>
        <servlet-name>connections</servlet-name>
        <servlet-class>com.apiEndpoints.Connections</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>connections</servlet-name>
        <url-pattern>/connections</url-pattern>
        <url-pattern>/connections/*</url-pattern>
    </servlet-mapping>
</web-app>
Matthew I
  • 1,043
  • 2
  • 10
  • 16
  • 1
    You're mixing JAX-RS with HttpServlet. `@GET`, `@Path` etc don't work at all on HttpServlet and are plain ignored. What exactly do you want? A JAX-RS resource or a web servlet? For one or other there are separate duplicate answers (and learning resources). – BalusC Mar 30 '16 at 18:22
  • I don't really grasp the difference between the two, but I'm trying to write a JAX-RS resource, since I'm trying to build a REST API – Matthew I Mar 30 '16 at 18:36
  • 1
    Which JAX-RS implementation do you have downloaded and installed? Jersey or RESTEasy? Or if you're using a real Java EE server like WildFly instead of servletcontainer like Tomcat, which Java EE server are you using? (as it has already JAX-RS builtin) – BalusC Mar 30 '16 at 19:12
  • I'm using Jersey 2.6 and I'm loading it onto Tomcat – Matthew I Mar 30 '16 at 19:20
  • 1
    Which of those is most helpful in getting started? http://stackoverflow.com/q/4474582 or http://stackoverflow.com/q/7874695? – BalusC Mar 30 '16 at 19:23
  • Got it working - the first link was more useful for the tutorial it had (and the web.xml in particular), but I used both. Thanks for your help. – Matthew I Mar 30 '16 at 20:15

0 Answers0