0

I've tried multiple reinstallations of tomcat and followed this tutorial: https://www.youtube.com/watch?v=5jQSat1cKMo

However I always get this error:

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

Screenshot here:

This is my current Hello.java class:

package test;

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

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

 @GET
 @Produces(MediaType.TEXT_PLAIN)
 public String sayHello() {
  String resource="This is some test return text";
  return resource;
 }
}

Current web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>JavaAPI</display-name>
  <servlet>
   <servlet-name>JavaAPI</servlet-name>
   <servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</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>JavaAPI</servlet-name>
   <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

I have tried with and without the extra imports of:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

What am I missing?

UPDATE:
I'm using plain using IDE
I am also using Jersey JAX-RS 2.1

CKE
  • 1,533
  • 19
  • 18
  • 29
  • Could you give more information like, did you use build tools such as maven/gradle or plain using IDE. Also please post the dependencies that you used on this project. – Sukma Wardana Jun 20 '18 at 07:48
  • @SukmaWardana I've updated the question. Im using plain IDE and Jersey JAX-RS 2.1 – Hamish Kendall Jun 20 '18 at 07:56
  • It might be due to Tomcat itself. Are you sure it's configured correctly? – Laurynas Jun 20 '18 at 07:56
  • Then how you manage the JAX-RS dependencies? Did you ship it with your project or put it on Apache Tomcat? For your information, Apache Tomcat is an servlet container only, I would suggest to look Apache TomEE because it's already shipped with JAX-RS dependencies. – Sukma Wardana Jun 20 '18 at 08:02
  • @Laurynas I can use Tomcat when I run it outside eclipse. https://i.imgur.com/VNJQWVT.jpg – Hamish Kendall Jun 20 '18 at 08:02
  • @SukmaWardana I copy the dependencies into my project library under WEB-INF / lib – Hamish Kendall Jun 20 '18 at 08:04
  • I see, you're deploying the project into tomcat trough Eclipse. Did you see any error message on the Eclipse console log when deploy and run your project? – Sukma Wardana Jun 20 '18 at 08:09
  • As @SukmaWardana pointed out, Tomcat is not a full blown Java EE server, so you have to set things little bit up, this post https://stackoverflow.com/questions/22083164/what-is-best-approach-for-implementing-jersey-2-x-on-tomcat-8 is quite comprehensive. – hradecek Jun 20 '18 at 08:11
  • Is there a reason you are using tomcat? In general I'd recommend using SpringBoot which embeds the server in the application. This is much easier to use. – Andreas Hartmann Jun 20 '18 at 08:20
  • @SukmaWardana No i didnt, however I have managed to get SpringBoot working. Thanks – Hamish Kendall Jun 20 '18 at 09:18
  • @AndreasHartmann I've just started using SpringBoot, thanks for the suggestion! – Hamish Kendall Jun 20 '18 at 09:19
  • nothing in this code is incompatible with Tomcat. Problem is in details – Jacek Cz Jun 20 '18 at 11:15

1 Answers1

0

This line in web.xml doesn't look right:

<servlet-class>org.glassfish.jersey.servlet.ServletContainer.class</servlet-class>

The Java Servlet Specification Version 4.0 (page 14-164) states:

The servlet-class contains the fully qualified class name of the servlet.

But what you have specified is a hybrid of the servlet's fully qualified class name and the servlet's file name, causing Tomcat to look for a servlet that doesn't exist, so you get a 404 error.

Just remove .class from the <servlet-class> value:

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
skomisa
  • 16,436
  • 7
  • 61
  • 102