0

I am trying to implement a web service with using jax-rs, jersey. I using IntelliJ, tomcat. Here is my web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>jersey sample</display-name>
  <servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>org.glassfish.jersey.config.property.packages</param-name>
// Here I am not sure which one is correct, main.java.controller or just
// controller? Controller is the class that I keep my endpoints (for 
// example the below controller) in it.
// (Both does not solve my problem)
      <param-value>main.java.controller</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
</web-app>

Controller:

@Path("/user")
public class UserRegistrationController {

@GET
@Path("/register")
@Consumes(APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response getSiteAllCallReport(@Context HttpServletRequest request, MultivaluedMap<String, String> form) throws SQLException, JSONException {
    JSONObject data = UserRegitrationProvider.registerUser(request, form);
    return Response.ok().entity(data.toString()).build();
}
}

When I run tomcat and type "localhost:8080/api/user/register" to the browser, I got HTTP 404 status. Where am I doing wrong?

JollyRoger
  • 737
  • 1
  • 12
  • 38
  • Do you get something at `localhost:8080/user/register` ? – sleeplessnerd Mar 29 '18 at 20:40
  • @sleeplessnerd No, it also gives 404 – JollyRoger Mar 29 '18 at 20:41
  • Maybe you dont even need a web.xml: https://stackoverflow.com/questions/9373081/how-to-set-up-jax-rs-application-using-annotations-only-no-web-xml/26721737#26721737 – sleeplessnerd Mar 29 '18 at 20:57
  • @sleeplessnerd Here in this answer, the guy said "http://localhost:8080/myContext/rest/...". Even there is not myContext or any other prefix, he wrote. Do I need something like this. And I do not have a class that extends Application. By the way deleting web.xml content does not worked. – JollyRoger Mar 29 '18 at 21:12
  • The context defaults to the name of your war file (sans the .war). So if your war is called `registration.war`, then the URL will be `http://localhost:8080/registration/api/user/register` – Steve C Mar 31 '18 at 01:48

0 Answers0