json - Jersey 2 InjectLink is ignored -
i'm creating new jersey 2.21, jackson 2.6.1 rest server (i tried jersey 2.19 , jackson 2.5.3 well) , want use @injectlink provide hateoas links (like 'self') callers. basic app (taken jersey doc , jersey sample apps) isn't working, , can't figure out why.
i started taking basic widgets class from jersey doc, json or xml returned empty structure.
xml:
<widgets/>
json:
{}
widgets.java
package org.glassfish.jersey.examples.linking; import java.net.uri; import java.util.list; import javax.ws.rs.core.link; import javax.xml.bind.annotation.xmlrootelement; import org.glassfish.jersey.linking.injectlink; import org.glassfish.jersey.linking.injectlinks; @xmlrootelement() public class widgets { @injectlink(resource=widgetsresource.class) uri u; @injectlinks({@injectlink(resource=widgetsresource.class, rel = "self")}) list<link> links; public widgets() {} }
i put breakpoint in resource code , examined object being returned. it.links , it.u both null. there's nothing injected.
widgetsresource.java
@path("widgets") public class widgetsresource { @get public widgets get() { widgets = new widgets(); return it; } }
maven dependencies:
<dependencies> <dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet</artifactid> <version>${jersey.version}</version> </dependency> <dependency> <groupid>org.glassfish.jersey.ext</groupid> <artifactid>jersey-declarative-linking</artifactid> <version>${jersey.version}</version> </dependency> <dependency> <groupid>org.glassfish</groupid> <artifactid>javax.el</artifactid> <version>2.2.4</version> </dependency> <dependency> <groupid>javax.el</groupid> <artifactid>javax.el-api</artifactid> <version>2.2.4</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.datatype</groupid> <artifactid>jackson-datatype-joda</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.jaxrs</groupid> <artifactid>jackson-jaxrs-json-provider</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.jaxrs</groupid> <artifactid>jackson-jaxrs-xml-provider</artifactid> <version>${jackson.version}</version> </dependency> <dependency> <groupid>com.fasterxml.jackson.jaxrs</groupid> <artifactid>jackson-jaxrs-yaml-provider</artifactid> <version>${jackson.version}</version> </dependency> </dependencies> <properties> <jersey.version>2.21</jersey.version> <jackson.version>2.6.1</jackson.version> </properties>
web.xml:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1"> <display-name>server</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey rest service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>javax.ws.rs.application</param-name> <param-value>org.glassfish.jersey.examples.linking.server</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>jersey rest service</servlet-name> <url-pattern>/rest/v1/*</url-pattern> </servlet-mapping> </web-app>
the application object:
package org.glassfish.jersey.examples.linking; import org.glassfish.jersey.linking.declarativelinkingfeature; import org.glassfish.jersey.server.resourceconfig; public class server extends resourceconfig { public server() { packages("org.glassfish.jersey.examples.linking", "com.fasterxml.jackson.jaxrs") .register(declarativelinkingfeature.class); } }
it appears @injectlink(s) annotations being ignored. there must magic setting i'm missing, i've done jersey doc says do.
i tried both servlet 2.x (shown) , servlet 3.x (using @applicationpath) , made no difference.
some of notice package names tried more advanced, no more successful, jersey declarative linking example (https://github.com/jersey/jersey/tree/master/examples/declarative-linking). of basic fields returned, of injected links missing or null.
.../server/rest/v1/items/1 produced:
{ "id": "1", "nextid": "2", "previd": "0", "prev": true, "next": true }
fyi - i'm running java 1.8.0_60, eclipse mars, , tomcat 8.0.26. there no errors, no warnings , no runtime messages. i'm stumped. have pointers???
i have added following servlet config:
`<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> org.glassfish.jersey.linking.declarativelinkingfeature </param-value> </init-param>'
and links rendered in json response.
Comments
Post a Comment