我有一个UserResource(root)类,如下所示:
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {
//....
// Subresource
@Path("/{userId}/adverts")
public AdvertResource getAdvertResource() {
return new AdvertResource();
}
}
AdvertResource类是一个子资源,处理来自http://localhost:8080/appName/api/users/1/adverts
的请求,如下所示:
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class AdvertResource {
//...
@GET
public List<Advert> getAll(@PathParam("userId") int userId) {
return this.advertFacade.findAllByUserId(userId);
}
}
getAll
方法从数据库中获取所有项目。在调试模式下,我可以看到userId是正确的,advertFacade
返回正确的广告列表。但我收到&#34; HTTP Status 500 - Internal Server Error: The server encountered an internal error that prevented it from fulfilling this request
&#34;在网络浏览器中。 serverlog中没有异常输出!
当我将方法改为这样的时候:
@GET
public String getAll(@PathParam("userId") int userId) {
return "User ID is: " + userId;
}
工作正常,我为http://localhost:8080/appName/api/users/99/adverts
输出User ID is: 99
我从原型创建项目:jersey-quickstart-ejb
org.glassfish.jersey.archetypes
我使用Glassfish 4.1。我的pom.xml看起来像这样:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>appName</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>appName</name>
<build>
<finalName>appName</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency> -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>