java 线程“main”中的异常 javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29438198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
提问by Alferd Nobel
I get the following exception when I execute the REST Client :
执行 REST Client 时出现以下异常:
InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.dcr.jersey.first/webapi/todo, status=406, reason=Not Acceptable}}
Exception in thread "main" javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
On web browser( when tomcat is running), the URL : http://localhost:8080/com.dcr.jersey.first/webapi/todogives output
在 Web 浏览器上(当 tomcat 运行时),URL:http://localhost:8080/com.dcr.jersey.first/webapi/todo给出输出
todo>
<description>This is my first todo - Description</description>
<summary>This is my first todo - Summary</summary>
</todo>
But running the client code throws the exception, what is the mapping that's missing here?, appreciate your guidance( included all code samples)
但是运行客户端代码会抛出异常,这里缺少什么映射?感谢您的指导(包括所有代码示例)
Here is the web.xml :
这是 web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</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.dcr.jersey</param-value> -->
<!-- <param-value>com.dcr.jersey.first</param-value> -->
<param-value>com.dcr.jersey.jaxb.model</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
Here is the TodoResourceCliient executed:
这是执行的 TodoResourceCliient:
package com.dcr.jersey.client;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class TodoResourceCliient {
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);
WebTarget target = client.target(getBaseURI());
System.out.println(target.path("webapi").path("todo").request()
.accept(MediaType.TEXT_PLAIN).get(Response.class)
.toString());
System.out.println(target.path("webapi").path("todo").request()
.accept(MediaType.TEXT_HTML).get(String.class));
System.out.println(target.path("webapi").path("todo").request()
.accept(MediaType.TEXT_XML).get(String.class));
System.out.println(target.path("webapi").path("todo").request()
.accept(MediaType.TEXT_PLAIN).get(String.class));
System.out.println(target.path("webapi").path("todo").request()
.accept(MediaType.APPLICATION_JSON).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/com.dcr.jersey.first").build();
}
}
TodoResource.java:
TodoResource.java:
package com.dcr.jersey.jaxb.model;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/todo")
public class TodoResource {
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Todo getXML() {
Todo todo = new Todo();
todo.setSummary("This is my first todo - Summary\n");
todo.setDescription("This is my first todo - Description\n");
return todo;
}
// This can be used to test the integration with the browser
@GET
@Produces({ MediaType.TEXT_XML,MediaType.TEXT_PLAIN,MediaType.TEXT_HTML})
public Todo getHTML() {
Todo todo = new Todo();
todo.setSummary("This is my first todo - Summary\n");
todo.setDescription("This is my first todo - Description\n");
return todo;
}
}
Todo.java:
待办事项.java:
package com.dcr.jersey.jaxb.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Todo {
private String summary;
private String description;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Stacktrace from Console :
来自控制台的堆栈跟踪:
InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.dcr.jersey.first/webapi/todo, status=500, reason=Internal Server Error}}
Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1002)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:799)
at org.glassfish.jersey.client.JerseyInvocation.access0(JerseyInvocation.java:91)
at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:687)
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:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:683)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:411)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:307)
at com.dcr.jersey.client.TodoResourceCliient.main(TodoResourceCliient.java:27)
Dependencies included from pom.xml: am I missing any dependencies ?
pom.xml 中包含的依赖项:我是否缺少任何依赖项?
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
采纳答案by Paul Samsotha
406 Not Acceptable
The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.
406 不可接受
请求标识的资源只能根据请求中发送的accept头生成具有不可接受的内容特征的响应实体。
The .accept(mediatype)
is what set the Accept
header for the request. You currently have five requests, each accepting a different type
这.accept(mediatype)
是Accept
为请求设置标头的内容。您目前有五个请求,每个请求都接受不同的类型
MediaType.TEXT_PLAIN
,MediaType.TEXT_HTML
,MediaType.TEXT_XML
,MediaType.TEXT_PLAIN
,MediaType.APPLICATION_JSON
MediaType.TEXT_PLAIN
,MediaType.TEXT_HTML
,MediaType.TEXT_XML
,MediaType.TEXT_PLAIN
,MediaType.APPLICATION_JSON
This is Content-Negotiationat work. The server side configuration of this is with the use of the @Produces
(which goes in hand with the Accept
header) and @Consumes
(which goes in hand with the Content-Type
header).
这是工作中的内容协商。这个服务器端的配置是使用@Produces
(它与Accept
标题相关)和@Consumes
(与Content-Type
标题相关)的使用。
That being said, look at all your @Produces
annotations. You currently only support the producing of media types
话虽如此,请查看所有@Produces
注释。您目前只支持制作媒体类型
MediaType.APPLICATION_XML
,MediaType.TEXT_XML
,MediaType.APPLICATION_JSON
MediaType.APPLICATION_XML
,MediaType.TEXT_XML
,MediaType.APPLICATION_JSON
The missing ones are in bold above. Remove those, and given all else is correct, this should work for you.
缺少的在上面以粗体显示。删除那些,如果其他一切都是正确的,这应该适合你。
回答by domih
Check your content type!
检查您的内容类型!
Setting to .accept(MediaType.TEXT_PLAIN)
worked for me.
设置.accept(MediaType.TEXT_PLAIN)
对我来说有效。
回答by Manish Soni
In plain language "HTTP 406 Not Acceptable" Means your request to server does not failed , while server is saying you that the content type of request which you have asked for cannot be served from backend or you need to provide right content type in header param . For reference of the header field you can see:- https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
用简单的语言“HTTP 406 Not Acceptable”表示您对服务器的请求没有失败,而服务器则告诉您无法从后端提供您请求的内容类型,或者您需要在标头参数中提供正确的内容类型. 有关标题字段的参考,您可以看到:- https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
See the response's content-type of the service and request for the same.
查看服务的响应内容类型和相同的请求。
回答by Abhishek
As correctly stated by peeskillet, your client calls with accept(MediaType.TEXT_PLAIN) and accept(MediaType.TEXT_HTML) will cause issues since your TodoResource methods do not specify these media types in the @Produces annotation.
正如peeskillet 正确指出的那样,您的客户端调用accept(MediaType.TEXT_PLAIN) 和accept(MediaType.TEXT_HTML) 会导致问题,因为您的TodoResource 方法没有在@Produces 注释中指定这些媒体类型。
Either
任何一个
- change your TodoResource class to support these media types
- change your client code to remove the calls corresponding to these media types
- 更改您的 TodoResource 类以支持这些媒体类型
- 更改您的客户端代码以删除与这些媒体类型对应的调用