我正在使用Netbeans的 RESTful Web服务开发Web服务。服务类包含以下方法:
public String jpost(Object requestEntity) throws ClientErrorException {
return webTarget.path("jpost").request(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
}
为了测试这种方法,我使用 Netbeans' RESTful Java Client 生成测试方法。结果是:
jpost
当我呼叫Exception in thread "main" javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:923)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:423)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:326)
at myserver.test5.jpost(test5.java:36)
at myserver.MyServerTest.main(MyServerTest.java:32)
时出现以下错误:
{{1}}
有什么问题?
答案 0 :(得分:1)
您的服务仅生成text/plain
个回复。
HTTP代码406表示服务器无法提供客户端可接受的内容类型。因此,客户端在Accepts
标头中指定了错误的内容类型,或者根本没有发送Accepts
。
要指定接受的类型,请使用request(...)
参数。
public String jpost(Object requestEntity) throws ClientErrorException
{
return webTarget.path("jpost")
.request(javax.ws.rs.core.MediaType.TEXT_PLAIN)
.post(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_JSON), String.class);
}