Эта вещь меня озадачивает уже довольно давно. Я пытаюсь встроить контейнер Джерси в Jetty. Следуя примерам, я могу включить Джерси, и я вижу, что мой открытый метод REST подвергается воздействию. однако, по какой-то странной причине, мой Джерси действительно возвращает 404. Похоже, что Джерси не в состоянии разрешить правильную конечную точку. К сожалению, это не говорит мне, почему, но просто бросает NotFoundException :-(.Джерси возвращается 404, после вызова правильного @Path
Я на 100% уверен, что мой метод вызывается. System.out.println внутри этого метода отображается в консоли, а мой Eclipse отладчик явно проходит множество точки останова не имеет значения, используете ли я Джерси 2,0, 2,3, 2.4,1, 2.4 Все они имеют один и тот же результат .. Вот фрагменты кода из моей конфигурации:..
StartJetty.java :
final int mainport = 9123;
Server jettyServer = new Server(mainport);
HandlerCollection handlerCollection = new ContextHandlerCollection();
jettyServer.setHandler(handlerCollection);
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/");
jerseyServlet.setInitOrder(1);
jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.test.rest");
handlerCollection.addHandler(context);
jettyServer.start();
jettyServer.join();
Отрывок из REST ресурса: com.test.rest.PersonsRSImpl.class:
@Path("/persons")
@Produces(MediaType.APPLICATION_JSON)
public class PersonsRSImpl {
private PersonManager personMgr = PersonManager.getInstance();
@Path("/list")
public Collection<Person> list() {
System.out.println("In Person::list");
return personMgr.getPersons();
}
}
Мой объект Person представляет собой простой POJO, с некоторыми примечаниями JAXB. Добавление/удаление не имеет никакого значения.
И последнее, но не менее важное: pom.xml http://maven.apache.org/maven-v4_0_0.xsd ">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>integrationexample</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>Test project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<jetty.version>7.5.4.v20111024</jetty.version>
<jersey.version>2.4.1</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http-spi</artifactId>
<version>${jetty.version}</version>
</dependency>
<!-- Jersey JAX-RS -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-simple-http</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.StartJetty</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Я включил след Джерси (спасибо . Михал) результат трассировки:
Джерси следа
HTTP/1.1 404 Not Found
X-Jersey-Tracing-000: START [ ----/---- ms | ---- %] baseUri=[http://localhost:9123/rest/] requestUri=[http://localhost:9123/rest/persons/list] method=[GET] authScheme=[n/a] accept=[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8] accept-encoding=[gzip, deflate] accept-charset=n/a accept-language=[en-US,en;q=0.5] content-type=n/a content-length=n/a
X-Jersey-Tracing-001: PRE-MATCH [ 0,01/1,03 ms | 0,10 %] PreMatchRequest summary: 0 filters
X-Jersey-Tracing-002: MATCH [ ----/1,21 ms | ---- %] Matching path [/persons/list]
X-Jersey-Tracing-003: MATCH [ ----/1,27 ms | ---- %] Pattern [/persons(/.*)?] IS selected
X-Jersey-Tracing-004: MATCH [ ----/1,41 ms | ---- %] Matched resource: template=[/persons] regexp=[/persons(/.*)?] matches=[/persons] from=[/persons/list]
X-Jersey-Tracing-005: MATCH [ ----/1,54 ms | ---- %] Matching path [/list]
X-Jersey-Tracing-006: MATCH [ ----/1,58 ms | ---- %] Pattern [/list(/.*)?] IS selected
X-Jersey-Tracing-007: MATCH [ ----/1,76 ms | ---- %] Matched resource: template=[/list] regexp=[/list(/.*)?] matches=[/list] from=[/list]
X-Jersey-Tracing-008: MATCH [ ----/1,90 ms | ---- %] Matched locator : public java.util.Collection com.test.rest.PersonsRSImpl.list()
X-Jersey-Tracing-009: MATCH [ ----/2,08 ms | ---- %] Resource instance: [com.test.rest.PersonsRSImpl @3cceafcb]
X-Jersey-Tracing-010: MATCH [ ----/2,21 ms | ---- %] Resource instance: [java.util.HashMap$Values @25591d82]
X-Jersey-Tracing-011: MATCH [ 4,23/5,30 ms | 75,92 %] RequestMatching summary
X-Jersey-Tracing-012: RESP-FILTER [ 0,00/5,52 ms | 0,05 %] Response summary: 0 filters
X-Jersey-Tracing-013: FINISHED [ ----/5,58 ms | ---- %] Response status: 404/CLIENT_ERROR|Not Found
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 1284
Server: Jetty(7.5.4.v20111024)
Надеюсь, у некоторых из вас есть ключ к тому, что происходит!
Можете ли вы включить [отслеживание поддержки] (https://jersey.java.net/documentation/latest/monitoring_tracing.html#tracing), чтобы узнать, что происходит после вызова метода? Достаточно добавить 'init.service.service.server.tracing' сервлета' jersey.config.server.tracing' со значением 'ALL' в вашу конфигурацию сервлета. –
Спасибо Michal за подсказку. Я добавил поддержку отслеживания, см. Мой первоначальный пост выше. – user2978801
Несвязанный напыщенный текст, но: 'Content-Type: text/html; charset = ISO-8859-1' ... Noo !! прекратите использование ISO-8859-1! :( –