Running REST with Java(JAX-RS) with Jersey on IntelliJ / Tomcat

12,103

Fixed the problem with this code.

github.com/silvae86

Share:
12,103
Philip
Author by

Philip

Updated on June 04, 2022

Comments

  • Philip
    Philip almost 2 years

    I'm trying to do the following tutorial in IntelliJ on OSX:

    http://www.vogella.com/tutorials/REST/article.html

    While running the code in Eclipse everything works fine. But running the code in IntelliJ delivers 404s for the same URI.

    The program should run on a Apache Tomcat Server 8.0.20.

    I've done exactly what was written in the Tutorial but i can't figure out why it won't work in IntelliJ. I've searched for days now, finding a solution.

    It looks like something with the deployment is wrong, because the index.jsp is working fine.

    Hope somebody can help me.

    Code of the program:

    Class Hello:

    package com.vogella.jersey.first;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    // Plain old Java Object it does not extend as class or implements 
    // an interface
    
    // The class registers its methods for the HTTP GET request using the @GET annotation. 
    // Using the @Produces annotation, it defines that it can deliver several MIME types,
    // text, XML and HTML. 
    
    // The browser requests per default the HTML MIME type.
    
    //Sets the path to base URL + /hello
    @Path("/hello")
    public class Hello {
    
        // This method is called if TEXT_PLAIN is request
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String sayPlainTextHello() {
            return "Hello Jersey";
        }
    
        // This method is called if XML is request
        @GET
        @Produces(MediaType.TEXT_XML)
        public String sayXMLHello() {
            return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
        }
    
        // This method is called if HTML is request
        @GET
        @Produces(MediaType.TEXT_HTML)
            public String sayHtmlHello() {
                return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
            }
    
    }
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <display-name>com.vogella.jersey.first</display-name>
        <servlet>
            <servlet-name>Jersey REST Service</servlet-name>
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <!-- Register resources and providers under com.vogella.jersey.first package. -->
            <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>com.vogella.jersey.first</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>Jersey REST Service</servlet-name>
            <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>
    </web-app> 
    

    Project structure: (Can't post pictures because of low reputation, Sorry I'm new)

    • rest
      • .idea
      • lib
        • javax.ws.rs-api-2.0.jar
        • jersey-client.jar
        • jersey-common.jar
        • jersey-container-servlet.jar
        • jersey-container-servlet-core.jar
        • jersey-server.jar
      • out
      • src
        • com.vogella.jersey.first
          • Hello
      • web
        • WEB-INF
          • web.xml
        • index.jsp
    • rest.iml
  • z3d0
    z3d0 about 8 years
    I'm trying your code but still getting 404 errors, what is the correct url for displaying the page? I tried localhost:8080/api/hello
  • Philip
    Philip about 8 years
    I am not 100% sure but I think it should be localhost:8080/rest/hello because of the <url-pattern>