我尝试用Jersey制作一个restfull Web服务。我想在该请求的回复中给出一个链接:
GET /mac/ws/gtm HTTP/1.1
Host: localhost:8080
Accept: application/json
Cache-Control: no-cache
我希望回复是:
HTTP/1.1 200 OK
link: </dossiers>;rel=dossiers
{
"message": "Hello"
}
但回应是:
HTTP/1.1 200 OK
{
"message": "Hello"
}
链接未生成!
查看我的Gtm资源:
@Component
@Path("/gtm")
public class GTmRessource
{
@GET
@Produces(MediaType.APPLICATION_JSON)
public GTm getJson()
{
GTm gtm = new GTm();
return gtm;
}
}
我的Gtm实体
@XmlRootElement()
@Link(value = @Ref(value = "/dossiers", method = "get"), rel = "dossiers")
public class GTm
{
String message = "Hello";
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
怎么了?
感谢您的帮助。 由
答案 0 :(得分:2)
请参阅Declarative Hyperlinking: Configuration
您需要以编程方式添加LinkFilter
:
resourceConfig.getContainerResponseFilters().add(LinkFilter.class);
或通过web.xml
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.server.linking.LinkFilter</param-value>
</init-param>
C:\>curl -i http://localhost:8080/gtm
HTTP/1.1 200 OK
Content-Type: application/json
Link: </dossiers>;rel=dossiers
Date: Thu, 04 Dec 2014 12:38:06 GMT
Transfer-Encoding: chunked
{"message":"Hello"}