在 Jersey 中注册 SelectableEntityFilteringFeature
我们需要做的就是以这种方式在 ResourceConfig类中注册 SelectableEntityFilteringFeature:
package com.onitroad.jersey;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.message.filtering.SelectableEntityFilteringFeature;
import org.glassfish.jersey.server.ResourceConfig;
public class CustomApplication extends ResourceConfig
{
public CustomApplication()
{
packages("com.onitroad.jersey");
// 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);
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "select");
}
}
如果你想通过 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-value>
</init-param>
要设置查询选择器属性,请使用以下代码:
<init-param>
<param-name>jersey.config.entityFiltering.selectable.query</param-name>
<param-value>select</param-value>
</init-param>
欢迎来到之路教程(on itroad-com)
测试 SelectableEntityFilteringFeature
- 点击 HTTP GET:http://localhost:8080/JerseyDemos/rest/employees
响应:
{"employeeList":[ {"id":1,"name":"JackLi Gupta"}, {"id":2,"name":"JackLi Kolenchiskey"}, {"id":3,"name":"Tomm Kameron"}]}
- 点击 HTTP GET:http://localhost:8080/JerseyDemos/rest/employees?select=employeeList,id
响应:
{"employeeList":[{"id":1},{"id":2},{"id":3}]}
- 点击 HTTP GET:http://localhost:8080/JerseyDemos/rest/employees/1
响应:
{"id":1,"name":"JackLi Gupta"}
- 点击 HTTP GET:http://localhost:8080/JerseyDemos/rest/employees/1?select=id
响应:
{"id":1}
- 点击 HTTP GET:http://localhost:8080/JerseyDemos/rest/employees/1?select=name
响应:
{"name":"JackLi Gupta"}
日期:2020-09-17 00:16:32 来源:oir作者:oir
