Jersey @Path not working in Jetty surf +

I cannot get the Jetty + servlet + Jersey combination. Here are my steps:

1.pom.xml

<packaging>war</packaging>
...
<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.17</version>
    </dependency>
</dependencies>

      

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" metadata-complete="false" version="3.1">
  <servlet>
    <servlet-name>org.example.MyApplication</servlet-name>
  </servlet>
  <servlet-mapping>
    <servlet-name>org.example.MyApplication</servlet-name>
    <url-pattern>/resources</url-pattern>
  </servlet-mapping>
</web-app>

      

3.sources

package org.example;

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

@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("org.example");
    }
}

package org.example;

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

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
    @GET
    public String get() {
        return "{\"hello\": 1337}";
    }
}

      

I am deploying a WAR created with by mvn clean compile package

copying it to a directory web-apps

. Accessing the following url returns my hard JSON:

http://localhost:8080/test-1.0-SNAPSHOT/resources
{"hello": 1337}

      

Now I change @Path

in my resource:

@Path("/bla")
@Produces(MediaType.APPLICATION_JSON)
public class MyResource {
    @GET
    public String get() {
        return "{\"hello\": 1337}";
    }
}

      

Now none of the following urls work:

http://localhost:8080/test-1.0-SNAPSHOT/resources/bla
http://localhost:8080/test-1.0-SNAPSHOT/bla
http://localhost:8080/test-1.0-SNAPSHOT/resources
http://localhost:8080/test-1.0-SNAPSHOT/resources/bla/
...

      

I tried several combinations, but I can't seem to get it to work.

+3


source to share


1 answer


Try adding this to your web.xml . This will scan the jersey bag and scan your bag org.example

.

<servlet>
        <servlet-name>Services</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>
                org.example
            </param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.scanning.recursive</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Services</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

      

You can see a fully working example here (defined for tomcat), but I have already successfully migrated it to the marina.



https://github.com/DominikAngerer/java-GsonJerseyProvider/blob/master/WebContent/WEB-INF/web.xml

Here's also a little help on loading - she uses Jersey for the dock API part - she also has a nice tutorial to help you a lot! https://github.com/amacoder/demo-restWS-spring-jersey-jpa2-hibernate

+3


source







All Articles