Reputation: 13
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.
Upvotes: 1
Views: 1659
Reputation: 209052
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.
Upvotes: 1