Jersey REST Web Service with Tomcat, Eclipse and 4

2019-08-16 00:20发布

This should be so simple, but its blowing my mind... I have read through hundreds of the questions here, and so far none of the responses has made a difference in what I have tried. It is obviously time for another set of eyes..

I am getting a 404 error when trying to do the most simple of rest tests.

My Tomcat 7.0 Servers starts up just fine. No errors.

My project name in Eclipse is: atomic_services_poc

The Context in Tomcat is set to: /atomic_services_poc I can successfully return static pages with: http://localhost:8080/atomic_services_poc/index.html

Here is my web.xml:

<servlet>
    <servlet-name>workflowmanagerserlvet</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.tp.wfm.webservice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>workflowmanagerserlvet</servlet-name>
    <url-pattern>/workflowmanager/*</url-pattern>
</servlet-mapping>

Here is the java code for WorkFlowManagerRestService.java

package com.tp.wfm.webservice;

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

@Path("/workflow")
public class WorkFlowManagerRestService {

    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello()
    {
        return "Data Return";
    }
}

I am using SoapUI to test the GET method of the restful request. I have tried numerous variations of the following and they all result in a 404 error.

GET host:8080/atomic_services_poc/workflowmanager/workflow

Help please before I throw this laptop...

3条回答
虎瘦雄心在
2楼-- · 2019-08-16 00:29

I don't see anything wrong in your setup, from what you show. You don't mention which Jersey version you are using but I guess is bigger than 2. (I believe in Jersey < 2 the name of the property in the web.xml was different).

I would suggest the following to try to debug:

(1) For the moment remove the

@Produces(MediaType.TEXT_XML)

as suggested by @braunpet, and just test it with your web browser

(2) Add a ResourceConfig class so that you can at least debug what the server is doing in startup. This is pretty simple, you define a class like this:

package com.tp.wfm.application;

import org.glassfish.jersey.server.ResourceConfig;


public class MyApplication extends ResourceConfig {

    public MyApplication() {
        super();

        packages("com.tp.wfm.webservice");
    }

}

And change your web.xml like this:

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.tp.wfm.application.MyApplication</param-value>
    </init-param>

Then you add a breakpoint in Eclipse to make sure your resources are loading.

I hope it helps!

查看更多
beautiful°
3楼-- · 2019-08-16 00:39

Ok, so this morning I went back to basics. I created a brand new workspace with eclipse on my laptop from the vogella tutorial. I got this one to work on my laptop in all of 10 minutes. I then went to my problem work area and added the same items step by step. It worked...

I then started adding in my components and slowly one by one, I was able to get them working. I must have had some type of spelling or something wrong, but I feel better that no one here could see it either.

The good news it is now working. (Even after I removed the vogella stuff)..

:)

Thanks all that helped!

查看更多
Anthone
4楼-- · 2019-08-16 00:43

well I had the same problem. I was looking for a solution for about 2 weeks. After having passed 20 tutorials it drived me crazy why my tomcat is still showing the 404 response. I just wanted to get a helloworld-example working but the server was showing the 404 every time when I tried to open a ResourcePATH (e.g. @Path("/hello")). My Web.xml was "setupped" for jersey 2.x and the url param was declared as "/rest/*". The URL was called correctly by localhost:PORT/PROJECT/rest/hello.. but no Chance.

Finally I found a solution for myself that may help someone else who is troubling with this (like the author of this question):

  • Create a .WAR file of your Project and put it into your "apache_tomcat/webapps" directory (Eclipse: Right Click on the Project-> Export-> as .WAR file)
  • Restart the Tomcat-Server
  • Run the Application on the restarted Server

After that, it worked for me, I was able to connect to any @Path("") I implemented. And if I want to implement a new @Path I have to reprocess the 3 steps. Hope this can be helpful!

查看更多
登录 后发表回答