[英]jersey 2.x selectable Entityfiltering
I am new in Jersey and I try to use theSelectableEntityFilteringFeature 我是新泽西州的新手,我尝试使用SelectableEntityFilteringFeature
I create a small service with the following classes/files: 我使用以下类/文件创建一个小型服务:
a resource file called EmployeeResource.java 一个名为EmployeeResource.java的资源文件
@
Path("employee")@ Produces("application/json")
public class EmployeeResource {
@
GET
public Employee getEmployee() {
System.out.println("-->Entered into getEmployee()");
Employee employee = new Employee();
employee.setId(123);
employee.setName("raj");
System.out.println(employee);
System.out.println("<--Exited from getEmployee()");
return employee;
}
}
a domain called Employee.java 一个名为Employee.java的域
@
XmlRootElement
public class Employee {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@
Override
public String toString() {
return "Employee [name=" + name + ", id=" + id + "]";
}
}
a web.xml file 一个web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</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.filter.jersey</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>
org.glassfish.jersey.filter.LoggingFilter,
org.glassfish.jersey.message.filtering.SelectableEntityFilteringFeature,
org.glassfish.jersey.jackson.JacksonFeature
</param-value>
</init-param>
<init-param>
<param-name>jersey.config.entityFiltering.selectable.query</param-name>
<param-value>select</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
a pom.xml file 一个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.howtodoinjava</groupId>
<artifactId>SelectableFiltering</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<properties>
<jersey2.version>2.19</jersey2.version>
<jaxrs.version>2.0.1</jaxrs.version>
</properties>
<dependencies>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<!-- Jersey 2.19 -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey2.version}</version>
</dependency>
</dependencies>
<build>
<finalName>JerseyDemos</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The test cases are below 测试用例如下
http://localhost:8080/SelectableFiltering/employee?select=id,name http:// localhost:8080 / SelectableFiltering / employee?select = id,名称
expected response { "id" : "123" "name": "raj" } 预期的响应{“ id”:“ 123”“ name”:“ raj”}
http://localhost:8080/SelectableFiltering/employee?select=id http:// localhost:8080 / SelectableFiltering / employee?select = id
expected response { "id" : "123" } 预期响应{“ id”:“ 123”}
It should return only the id but it return everything. 它应该仅返回id,但返回所有内容。 I can't find a solution by searching the internet but maybe anybody here can tell me what i am doing wrong.
我无法通过搜索互联网找到解决方案,但也许任何人都可以告诉我我做错了什么。
and also getting 500 as response when I accessing the above resources ie 当我访问上述资源时也得到500作为响应,即
http://localhost:8080/SelectableFiltering/employee?select=id http://localhost:8080/SelectableFiltering/employee?select=id,name http:// localhost:8080 / SelectableFiltering / employee?select = id http:// localhost:8080 / SelectableFiltering / employee?select = id,名称
the stacktrace below Jul 27, 2016 8:48:21 PM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.filter.jersey.Employee, genericType=class com.filter.jersey.Employee. 2016年7月27日8:48:21 PM下方的堆栈跟踪,genericType = class com.filter.jersey.Employee。 Jul 27, 2016 8:48:21 PM org.glassfish.jersey.filter.LoggingFilter log INFO: 1 * Server responded with a response on thread http-nio-8080-exec-12 1 < 500
2016年7月27日8:48:21 PM org.glassfish.jersey.filter.LoggingFilter日志信息:1 *服务器对线程http-nio-8080-exec-12的响应进行了响应1 <500
here can tell me what i am doing wrong. 在这里可以告诉我我在做什么错。
If you want to send JSON and HTTP Status together you can use Response as your return type. 如果要一起发送JSON和HTTP状态,则可以使用Response作为返回类型。
@GET
public Response getEmployee() {
System.out.println("-->Entered into getEmployee()");
Employee employee = new Employee();
employee.setId(123);
employee.setName("Raju");
System.out.println(employee);
System.out.println("<--Exited from getEmployee()");
return Response.ok(employee.toString(), MediaType.APPLICATION_JSON).build();
}
Don't forget to add 不要忘记添加
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.