在泽西岛使用SelectableEntityFiltering 2.17 [英] Using SelectableEntityFiltering in Jersey 2.17

查看:185
本文介绍了在泽西岛使用SelectableEntityFiltering 2.17的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新泽西州我是新来的,我尝试使用示例中的SelectableEntityFilteringFeature:
http://blog.dejavu.sk/2015/02/04/jerseys-entity-filtering-meets-jackson/#selectable



我使用以下类/文件创建一个小型服务:
$ b

一个名为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 {
$ b $ public PersonResource {

//注册包下的所有资源。
包(org.glassfish.jersey.examples.entityfiltering.selectable);

//注册实体过滤可选功能。
寄存器(SelectableEntityFilteringFeature.class);
属性(SelectableEntityFilteringFeature.QUERY_PARAM_NAME,select);

//配置MOXY JSON提供程序。评论这条线使用杰克逊。取消注释使用MOXY。
// register(new MoxyJsonConfig()。setFormattedOutput(true).resolver());

//配置Jackson Json提供程序。注释这一行使用MOXY。取消注释使用杰克逊。
寄存器(JacksonFeature.class);


$ b @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的域 em>

  import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement
public class Person {
private String givenName;

私人字符串familyName;

私人字符串honorificSuffix;

私人字符串honorificPrefix;

//和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>原型创建的Web应用程序< / display-name>
< servlet>
< servlet-name> Jersey REST服务< / 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>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name> Jersey REST服务< / servlet-name>
< url-pattern> / service / *< / url-pattern>
< / servlet-mapping>
< / web-app>

对JSON的对象序列化对Jackson和Moxy来说也非常合适。但是,返回值的选择不起作用。我想这个网址:



http :// localhost:8080 / person / service / person& select = familyName

应该只返回姓氏,但是一切都是回报。
我无法找到一个解决方案,通过搜索互联网,但也许有人在这里可以告诉我,我做错了什么。

解决方案

问题似乎是你错误地使用了 ResourceConfig 。您可以查看 ResourceConfig 作为使用web.xml的替代方法。它应该由作为应用程序配置类(而不是由您的资源类)服务器的类进行扩展。例如

  @ApplicationPath(/ service)
public class AppConfig extends ResourceConfig {
public AppConfig (){
packages(com.servicetest.person.resources);
寄存器(LoggingFilter.class);




$ p $这是一个类似于你的web.xml的配置,其中 @ApplicationPath(/ service)相当于< url-pattern> packages(..)的作用与您的< init-param> 相同。为了使这个类能够工作,就像上面的例子一样,你应该在web.xml中注释整个servlet的定义。然后你可以添加属性和功能到构造函数
$ b $ pre $ public AppConfig(){
packages(com.servicetest .person.resources);
寄存器(LoggingFilter.class);

寄存器(SelectableEntityFilteringFeature.class);
属性(SelectableEntityFilteringFeature.QUERY_PARAM_NAME,select);
寄存器(JacksonFeature.class);





$如果你想坚持使用web.xml,那么你可以添加功能到提供者类名列表,即

 < 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值>
< / init-param>

为了设置查询选择器属性,请看常量字段值,用于 SelectableEntityFilteringFeature.QUERY_PARAM_NAME 。您可以使用这个值来设置< init-param>

 < INIT-PARAM> 
< param-name> jersey.config.entityFiltering.selectable.query< / param-name>
< param-value>选择< /参数值>
< / init-param>

然后在 PersonResource (并删除 ResourceConfig 扩展名),并且应该设置。


I am new in Jersey and i try to use the SelectableEntityFilteringFeature from the example: http://blog.dejavu.sk/2015/02/04/jerseys-entity-filtering-meets-jackson/#selectable

I create a small service with the following classes/files:

a resource file called 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;
    }
}

a domain called 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;
    }
}

a web.xml file

<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>

The object serialization to JSON works very well with Jackson and Moxy also. But the selection for the return values does not work. I think this URL:

http://localhost:8080/person/service/person&select=familyName

shoud return only the family name but everything is in return. I cant find a solution by searching the internet but maybe anybody here can tell me what i am doing wrong.

解决方案

Problem seems to be your incorrect use of ResourceConfig. You can look at ResourceConfig as an alternative to using web.xml. It should be extends by the class that will server as the application configuration class (not by your resource classes). For example

@ApplicationPath("/service")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("com.servicetest.person.resources");
        register(LoggingFilter.class);
    }
}

This is a similar configuration to your web.xml, where @ApplicationPath("/service") is equivalent to the <url-pattern> and the packages(..) serves the same purpose as your <init-param>. For this class to work, as in the example above, you should get comment out the the whole servlet definition in the web.xml. Then you can add the property and feature the to constructor

public AppConfig() {
    packages("com.servicetest.person.resources");
    register(LoggingFilter.class);

    register(SelectableEntityFilteringFeature.class);
    property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
    register(JacksonFeature.class);
}

If you want to stick with the web.xml, then you can add the feature to the list of provider class names, i.e.

<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>

And for setting the query selector property, look at the Constant Field Values for the String value of the SelectableEntityFilteringFeature.QUERY_PARAM_NAME. You can use this value to set an <init-param>

<init-param>
    <param-name>jersey.config.entityFiltering.selectable.query</param-name>
    <param-value>select</param-value>
</init-param>

Then just get rid of the whole constructor in the PersonResource (and get rid of the ResourceConfig extension), and you should be set.

这篇关于在泽西岛使用SelectableEntityFiltering 2.17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆