2

I am trying to write a Java web application that provides both HTML and REST interface. I would like to create a servlet that would provide the HTML interface using JSP, but data should also be accessible via REST.

What I already have is something like this for the REST:

@javax.ws.rs.Path("/api/")
public class RestAPI {

   ... // Some methods
}

and

@WebServlet("/servlet") 
public class MyServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.getWriter().write("Howdy at ");
    }
}

Now, when I change the @WebServlet("/servlet") annotation to @WebServlet("/"), the servlet stops working probably due to path clash with the REST.

How can I provide REST on specific path and HTML in the root?

Thank you, Lukas Jendele

  • BTW, it should be possible to serve the HTML interface by JAX-RS. In fact, it should be even possible to serve the HTML interface on the exact same URLs as the API -- I did't try that with Swarm, but I've seen it done with Jetty + Jersey, so I can't see why it couldn't work with Swarm too. – Ladicek Oct 25 '16 at 08:18

1 Answers1

2

This seems to work OK for me. What I did:

  1. In my pom.xml, I have a dependency on org.wildfly.swarm:undertow (for Servlet API) and org.wildfly.swarm:jaxrs (for JAX-RS). And of course the Swarm Maven plugin.

  2. For servlet, I have just this one class:

@WebServlet("/")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("Hello from servlet");
    }
}
  1. For JAX-RS, I have these 2 classes:
@ApplicationPath("/api")
public class RestApplication extends Application {
}
@Path("/")
public class HelloResource {
    @GET
    public Response get() {
        return Response.ok().entity("Hello from API").build();
    }
}

To test, I run curl http://localhost:8080/ and curl http://localhost:8080/api. Results are as expected. (Maybe my example is too simple?)

Ladicek
  • 5,970
  • 17
  • 20
  • Thanks! I tried exactly what you posted with the only difference was that I am using gradle instead of maven. It doesn't work. I included the gradle and main file in the question, so you can take a look. – Lukas Jendele Oct 25 '16 at 09:10
  • Thank you very much. The problem was that I was missing the RestApplication class. The example you posted works great. Is it possible to specify multiple GET methods in one class and specify the @@Path for the methods and not for the class? I tried to move the Path annotation to the method, however, that doesn't work for me. – Lukas Jendele Oct 25 '16 at 09:26
  • I figured it out eventually. One has to set the path annotation for both the class and the methods. – Lukas Jendele Oct 25 '16 at 09:28
  • Can you please post your pom.xml file? I trying to migrate from Gradle to Maven and have some difficulties. Thank you. – Lukas Jendele Nov 19 '16 at 12:36
  • 1
    I've got a couple of "playground" Swarm projects over here: https://github.com/ladicek/swarm-tinies All their pom.xml files look almost the same. – Ladicek Nov 19 '16 at 13:19