404 when calling Jersey 2 REST endpoint on Glassfish

独自空忆成欢 提交于 2020-01-01 07:22:47

问题


This is my web.xml

<servlet>
        <servlet-name>Simulator HTTP API</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Simulator HTTP API</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

and this is my simple web service:

@Path("partner")
public class PartnerAPI {

    @Path("/mt")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sendMT() {
        return "Sent";
    }

}

when i call it like this:

http://localhost:8080/myprojectname/partner/mt

i get 404 error mot found, what am i doing wrong?

Update

this is my maven

<dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</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>

回答1:


You have different deployment options in Jersey 2:

If you want to do it via web.xml you have to add the an init-param where you specify which packages should be scanned:

<servlet>
        <servlet-name>Simulator HTTP 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>insert.packagename.where.your.class.is.here</param-value>
        </init-param>
</servlet>

Another option is to create a basic class to configure your REST application.

This would look like this:

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;

    @ApplicationPath("/test")
    public class YourApplication extends ResourceConfig {
        public YourApplication() {
            this.packages("insert.packagename.where.your.class.is.here");
        }
    }

Make sure to update the string with the package name where your PartnerAPI class is.

Then add the value inside @ApplicationPath to your URL.
The link would look like this: http://localhost:8080/myprojectname/test/partner/mt

More information: Jersey docs: Chapter 4. Deploying a RESTful Web Service



来源:https://stackoverflow.com/questions/33173904/404-when-calling-jersey-2-rest-endpoint-on-glassfish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!