2
votes

I have looked at all the answers available on this topic, either I am facing a completely different problem or I am missing something big time.

Service Class:

package org.test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

//http://localhost:8080/JunkWeb/rest/TestRestService/callService
@Path("/TestRestService")
public class TestRestService {

    @GET
    @Path("/callService")
    @Produces(MediaType.TEXT_PLAIN)
    public String callService(){return "from Rest Service";}

}//class closing

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" version="3.0">
  <display-name>JunkWeb</display-name>

  <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>org.test</param-value>
    </init-param>
    <!-- <init-param>
      <param-name>jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</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>

The URL:

http://localhost:8080/JunkWeb/rest/TestRestService/callService

enter image description here

Should work but it does not. Any help will be really appreciated.

I am using Jersey 2.17 and Tomcat 8.0.20

1

1 Answers

3
votes

"I am using Jersey 2.17"

This does not exist in 2.17

com.sun.jersey.spi.container.servlet.ServletContainer

I'm surprised you're not getting a class not found exception. Which means you are probably mixing versions. Or maybe you're getting an exception an not telling us. In any case, the correct ServletContainer should be

org.glassfish.jersey.servlet.ServletContainer

Next what you should do, if you have any, is get rid of everything (jar) whose packages begin with com.sun. These are Jersey 1 jars. In Jersey two, the package naming pattern changed to org.glassfish.xxx If you want to make life easy, use Maven, and simply add just one dependency to whole project, and it will pull in all the rest.

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.17</version>
</dependency>

Also, this doesn't exist in Jersey two either

jersey.api.json.POJOMappingFeature

In Jersey 2, simply add this Maven depedency, and life will be good.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.17</version>
</dependency>

If you're not using Maven, download the RI bundle here. Open all the folders and add every jar to your project. This is for core support.

For JSON support, download this, as well as all of these. You can search the same site for them. doing this should work with no extra configuration. Alternatively, you can just download only the ones in the second link, then add the package in the web.xml, as seen in the link.

But just to get it working, since your code doesn't produce or consume any JSON, you can simply get the core running first, then once it starts working correctly, you can work on the JSON support.