Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 734
Philip Avatar asked Nov 10 '22 17:11

Philip


1 Answers

Fixed the problem with this code.

github.com/silvae86

like image 57
Philip Avatar answered Nov 14 '22 21:11

Philip