2

I downloaded the project from mkyong's website. I'm facing issue running the project. I'm not sure what is wrong. I'm doing this the first time, so please help me. If this works, I want to add hibernate to this project.

I tried to deploy this in Glassfish server and Wildfly 10. Didn't work in either of the servers

pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.common</groupId>
    <artifactId>RESTfulExample</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>RESTfulExample Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.22.1</version>
        </dependency>



    </dependencies>

    <build>
        <finalName>RESTfulExample</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

HelloWorldService.java

package com.mkyong.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {

        String output = "Jersey say : " + msg;

        return Response.status(200).entity(output).build();

    }

}

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>

    <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.mkyong.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

web.xml enter image description here

user525146
  • 3,918
  • 14
  • 60
  • 103

1 Answers1

1

You are using Jersey 1.x configuration for Jersey 2.x dependencies. It won't work. The two major versions have no compatibility whatsoever. 2.x configuration looks more like

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            com.mkyong.rest
        </param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

See more at Servlet-based Deployment.

A few other things:

  • You are already using Maven. No need to put jars into the WEB-INF/lib yourself.

  • If you are using Glassfish, you should put all the Jersey dependencies you are using in a provided <scope>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.22.1</version>
        <scope>provided</scope>
    </dependency>
    

    The reason is that Glassfish already uses Jersey as the JAX-RS implementation, but it uses an older version (with GF 4.1 its 2.10.4). You don't want a different version to cause conflicts. If you want to upgrade Jersey in Glassfish, you can check out Updating Jersey 2 in GlassFish 4

  • If you want to use Wildfly, it already uses RESTeasy as JAX-RS implementation. To use Jersey you should disable RESTeasy. Not completely sure the best way to go about doing that. You may need to do some googling.

  • If you were to just use a plain servlet container like Tomcat, you can leave the dependencies as is, without <scope>provided</scope>, as Tomcat doesn't have a JAX-RS implementation.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • It didn't work for me, I changed servlet-class to `org.glassfish.jersey.servlet.ServletContainer`, and also param-name to `jersey.config.server.provider.packages` but no luck – user525146 Nov 02 '15 at 05:31
  • What server are you using? – Paul Samsotha Nov 02 '15 at 05:33
  • Also you need to add a "param" to the end of your URL, for example `/rest/hello/something`, because of `@Path("/{param}")` – Paul Samsotha Nov 02 '15 at 05:36
  • i'm using Pivotal tc Server Deployment that came with Spring Source Tool Suite. the URL I have is `http://localhost:8080/RESTfulExample/rest/hello/world` – user525146 Nov 02 '15 at 05:42
  • With your current pom.xml file, and the new web.xml, and taking out the jars in the lib, there's no reason it shouldn't work, unless there is a deployment issue with the IDE/server – Paul Samsotha Nov 02 '15 at 05:47
  • Is there anything in the server log when you launch the server that says there is a problem? – Paul Samsotha Nov 02 '15 at 05:49
  • The jars are created when I ran the Maven Install in the eclipse. Do I have to mvn clean and deploy to the server ? – user525146 Nov 02 '15 at 05:49
  • I don't know. It's been a while since I used eclipse. I didn't know it copies the jars to the lib. I know Maven should copy them to the final war, but I didn't know that eclipse will display them in the project. – Paul Samsotha Nov 02 '15 at 05:51
  • One thing to note is that if what is in your image of the lib is what Eclipse copies and uses, then not all the jars are getting copied. Those are not all the jars that come with all the Maven dependencies you declared. And it is not enough jars to run your app. The image is missing some Jersey jars – Paul Samsotha Nov 02 '15 at 05:53
  • Though it may not solve your problem, you can check out [this answer](http://stackoverflow.com/a/33327308/2587435) for a quick sanity check to see it works, when not using your IDE/IDE-server – Paul Samsotha Nov 02 '15 at 05:57
  • Also, if for any reason you didn't create the project correctly, you see [here](http://stackoverflow.com/a/30020830/2587435) for easy to create a new project. Try to run that project and see what happens – Paul Samsotha Nov 02 '15 at 06:15
  • Creating the project as jersey archetype worked perfectly. Thanks ! :) – user525146 Nov 02 '15 at 07:09