0

I am quite new in programming, I am trying to create my first RESTfull API. I have created a server with Apache Tomcat/8.5.37 and I used jersey RESTfull web services. My problem is that class content (XML) is not printed on localhost:8080 using the path http://localhost:8080/JavaAPI/rest/hello

I created a tomcat server and used both JAX-RS 2.0 / Jersey 2.25.x and JAX-RS 2.1 / Jersey 2.26+ in the case of a versioning problem, none of them works. I just cannot think of what could be the problem.

package test;

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

@Path("/hello")

public class Hello {

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHello(){
        String resource ="<? xml version='1.0' ?>" +
                "<hello>, Hi, its  XML</hello>";
        return resource;
    }
}

This is the configuration of REST service file:

<display-name>JavaAPI</display-name>
  <servlet>
    <servlet-name>JAVA API</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>test</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup> 
  </servlet>
    <servlet-mapping>
     <servlet-name>JAVA API</servlet-name>
     <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

This is a screenshot of current jersey's library:

lib libcont

The expected outcome should be: , Hi, its XML to be printed on http://localhost:8080/JavaAPI/rest/hello and i get the follow 404 error : Type: Status Report

Message: Not Found

Description: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Reason why this might not be duplicated question: Firstly, i have copy / pasted their code along with jerseys older version 1.x and the issue still remained. In my experience i believe there is issue on versioning since i have been using Tomcat 8.5 and i also get the following response from server:

TomcaterrTomcaterrcont

The guide that i have followed is : https://www.youtube.com/watch?v=5jQSat1cKMo&t=782s&list=LL2fRfBs2m2v1wyy_kKt8E9w&index=2

funnyguy
  • 13
  • 5
  • How is your REST service configured? Are you sure about base path? – Andrei Makarevich Jan 27 '19 at 10:29
  • Just updated it. I believe the path is fine from JAVA API and /rest/* – funnyguy Jan 27 '19 at 10:55
  • Possible duplicate of [Getting 404 error with Jersey Tomcat service](https://stackoverflow.com/questions/30422448/getting-404-error-with-jersey-tomcat-service) – Andrei Makarevich Jan 27 '19 at 12:00
  • Can you provide the complete log output for the server startup? Are you following a guide? – nitind Jan 27 '19 at 14:35
  • 1) You don't need to extend Application. 2) You are using the wrong init-param name. The one you are using is for Jersey 1.x. The one you need to be using is `jersey.config.server.provider.packages`. This is the property for Jersey 2.x. That should do the trick for you. – Paul Samsotha Jan 28 '19 at 02:12
  • I just updated what you suggested still it does not work for me. Probably some issues with the configuration of the server. – funnyguy Jan 31 '19 at 17:05

1 Answers1

0

Try to change init-param configuration like this.

<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>Test</param-value>
</init-param>

Also consider changing package name to follow Java naming conventions:

Packages: The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, like com, edu, gov, mil, net, org. Subsequent components of the package name vary according to an organisation’s own internal naming conventions.

Examples:

com.sun.eng
com.apple.quicktime.v2

// java.lang packet in JDK
java.lang

The problem is also in mapping. Try to check

http://localhost:8080/JavaAPI/hello