在 Jersey 2.17 中使用 SelectableEntityFiltering
Using SelectableEntityFiltering in Jersey 2.17
我是泽西岛的新手,我尝试使用示例中的 SelectableEntityFilteringFeature:
http://blog.dejavu.sk/2015/02/04/jerseys-entity-filtering-meets-jackson/#selectable
我用以下 classes/files 创建了一个小服务:
一个名为 PersonResource.java
的资源文件
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.message.filtering.SelectableEntityFilteringFeature;
import org.glassfish.jersey.server.ResourceConfig;
@Path("/")
@Produces("application/json")
public class PersonResource extends ResourceConfig{
public PersonResource() {
// Register all resources present under the package.
packages("org.glassfish.jersey.examples.entityfiltering.selectable");
// Register entity-filtering selectable feature.
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
// Configure MOXy Json provider. Comment this line to use Jackson. Uncomment to use MOXy.
//register(new MoxyJsonConfig().setFormattedOutput(true).resolver());
// Configure Jackson Json provider. Comment this line to use MOXy. Uncomment to use Jackson.
register(JacksonFeature.class);
}
@GET
@Path("person")
public Person getPerson(){
final Person person = new Person();
person.setGivenName("Andrew");
person.setFamilyName("Dowd");
person.setHonorificPrefix("Mr.");
person.setHonorificSuffix("PhD");
return person;
}
}
一个名为 Person.java
的域
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person {
private String givenName;
private String familyName;
private String honorificSuffix;
private String honorificPrefix;
// same name as Address.region
private String region;
public String getGivenName() {
return givenName;
}
public void setGivenName(String givenName) {
this.givenName = givenName;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public String getHonorificSuffix() {
return honorificSuffix;
}
public void setHonorificSuffix(String honorificSuffix) {
this.honorificSuffix = honorificSuffix;
}
public String getHonorificPrefix() {
return honorificPrefix;
}
public void setHonorificPrefix(String honorificPrefix) {
this.honorificPrefix = honorificPrefix;
}
}
一个web.xml文件
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Jersey REST Service</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.servicetest.person.resources</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.examples.entityfiltering.selectable;org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
JSON 的对象序列化也适用于 Jackson 和 Moxy。但是 return 值的选择不起作用。我认为这个 URL:
http://localhost:8080/person/service/person&select=familyName
应该 return 只有姓氏,但一切都在 return 中。
我无法通过搜索互联网找到解决方案,但也许这里的任何人都可以告诉我我做错了什么。
问题似乎是您对 ResourceConfig
的错误使用。您可以将 ResourceConfig
视为使用 web.xml 的替代方法。它应该由将作为应用程序配置 class 服务的 class 扩展(而不是由您的资源 classes)。例如
@ApplicationPath("/service")
public class AppConfig extends ResourceConfig {
public AppConfig() {
packages("com.servicetest.person.resources");
register(LoggingFilter.class);
}
}
这是与您的 web.xml 类似的配置,其中 @ApplicationPath("/service")
等同于 <url-pattern>
,而 packages(..)
与您的 [=19= 具有相同的用途].要使 class 正常工作,如上例所示,您应该在 web.xml 中注释掉整个 servlet 定义。然后你可以添加 属性 并将其添加到构造函数
public AppConfig() {
packages("com.servicetest.person.resources");
register(LoggingFilter.class);
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
register(JacksonFeature.class);
}
如果您想坚持使用 web.xml,则可以将该功能添加到提供商 class 名称列表中,即
<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>
对于设置查询选择器 属性,查看 Constant Field Values 以获得 SelectableEntityFilteringFeature.QUERY_PARAM_NAME
的字符串值。您可以使用此值设置 <init-param>
<init-param>
<param-name>jersey.config.entityFiltering.selectable.query</param-name>
<param-value>select</param-value>
</init-param>
然后只需删除 PersonResource
中的整个构造函数(并删除 ResourceConfig
扩展名),你就应该设置好了。
我是泽西岛的新手,我尝试使用示例中的 SelectableEntityFilteringFeature: http://blog.dejavu.sk/2015/02/04/jerseys-entity-filtering-meets-jackson/#selectable
我用以下 classes/files 创建了一个小服务:
一个名为 PersonResource.java
的资源文件import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.message.filtering.SelectableEntityFilteringFeature;
import org.glassfish.jersey.server.ResourceConfig;
@Path("/")
@Produces("application/json")
public class PersonResource extends ResourceConfig{
public PersonResource() {
// Register all resources present under the package.
packages("org.glassfish.jersey.examples.entityfiltering.selectable");
// Register entity-filtering selectable feature.
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
// Configure MOXy Json provider. Comment this line to use Jackson. Uncomment to use MOXy.
//register(new MoxyJsonConfig().setFormattedOutput(true).resolver());
// Configure Jackson Json provider. Comment this line to use MOXy. Uncomment to use Jackson.
register(JacksonFeature.class);
}
@GET
@Path("person")
public Person getPerson(){
final Person person = new Person();
person.setGivenName("Andrew");
person.setFamilyName("Dowd");
person.setHonorificPrefix("Mr.");
person.setHonorificSuffix("PhD");
return person;
}
}
一个名为 Person.java
的域import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Person {
private String givenName;
private String familyName;
private String honorificSuffix;
private String honorificPrefix;
// same name as Address.region
private String region;
public String getGivenName() {
return givenName;
}
public void setGivenName(String givenName) {
this.givenName = givenName;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public String getHonorificSuffix() {
return honorificSuffix;
}
public void setHonorificSuffix(String honorificSuffix) {
this.honorificSuffix = honorificSuffix;
}
public String getHonorificPrefix() {
return honorificPrefix;
}
public void setHonorificPrefix(String honorificPrefix) {
this.honorificPrefix = honorificPrefix;
}
}
一个web.xml文件
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Jersey REST Service</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.servicetest.person.resources</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.examples.entityfiltering.selectable;org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
JSON 的对象序列化也适用于 Jackson 和 Moxy。但是 return 值的选择不起作用。我认为这个 URL:
http://localhost:8080/person/service/person&select=familyName
应该 return 只有姓氏,但一切都在 return 中。 我无法通过搜索互联网找到解决方案,但也许这里的任何人都可以告诉我我做错了什么。
问题似乎是您对 ResourceConfig
的错误使用。您可以将 ResourceConfig
视为使用 web.xml 的替代方法。它应该由将作为应用程序配置 class 服务的 class 扩展(而不是由您的资源 classes)。例如
@ApplicationPath("/service")
public class AppConfig extends ResourceConfig {
public AppConfig() {
packages("com.servicetest.person.resources");
register(LoggingFilter.class);
}
}
这是与您的 web.xml 类似的配置,其中 @ApplicationPath("/service")
等同于 <url-pattern>
,而 packages(..)
与您的 [=19= 具有相同的用途].要使 class 正常工作,如上例所示,您应该在 web.xml 中注释掉整个 servlet 定义。然后你可以添加 属性 并将其添加到构造函数
public AppConfig() {
packages("com.servicetest.person.resources");
register(LoggingFilter.class);
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
register(JacksonFeature.class);
}
如果您想坚持使用 web.xml,则可以将该功能添加到提供商 class 名称列表中,即
<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>
对于设置查询选择器 属性,查看 Constant Field Values 以获得 SelectableEntityFilteringFeature.QUERY_PARAM_NAME
的字符串值。您可以使用此值设置 <init-param>
<init-param>
<param-name>jersey.config.entityFiltering.selectable.query</param-name>
<param-value>select</param-value>
</init-param>
然后只需删除 PersonResource
中的整个构造函数(并删除 ResourceConfig
扩展名),你就应该设置好了。