2015-04-03 172 views
3

我出現以下情況例外,當我執行REST客戶端:異常線程「main」 javax.ws.rs.NotAcceptableException:HTTP 406不可接受

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 

在網頁瀏覽器(Tomcat的運行時),網址: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> 

但在運行客戶端代碼拋出異常,那是缺少這裏的映射?感謝您的指導(包括所有的代碼示例)

這裏是在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> 

這裏是執行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:

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:

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; 
    } 


} 

堆棧從控制檯跟蹤:從pom.xml中包含

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.access$500(JerseyInvocation.java:91) 
    at org.glassfish.jersey.client.JerseyInvocation$2.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> 
     <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> 

回答

3

參見Http Status Codes

406不可接受

該請求所標識的資源只能能夠產生具有根據在發送的接受報頭,內容無法接收響應實體的請求。

.accept(mediatype)是爲請求設置Accept標頭。您目前有五個申請,每個接受不同類型

MediaType.TEXT_PLAINMediaType.TEXT_HTMLMediaType.TEXT_XMLMediaType.TEXT_PLAINMediaType.APPLICATION_JSON

這是Content-Negotiation工作。此服務器端配置使用@Produces(與Accept標頭一起使用)和@Consumes(與Content-Type標頭一起使用)。

這就是說,看看你所有的@Produces註釋。您目前僅支持媒體類型

MediaType.APPLICATION_XMLMediaType.TEXT_XML的生產中,MediaType.APPLICATION_JSON

沒有的部分以粗體以上。刪除這些,並給所有其他的是正確的,這應該爲你工作。

+0

感謝您的清除。正如我指出的那樣,我評論了粗體的MediaType。當我運行代碼時,我得到一個Http 500(內部服務器異常)。這是我做的: – 2015-04-05 18:44:50

+0

當我運行代碼時,我得到一個Http 500(內部服務器異常)。 MediaType.TEXT_XML已成功執行。 MediaType.APPLICATION_JSON拋出:[異常在線程「主」「javax.ws.rs.InternalServerErrorException:HTTP 500內部服務器錯誤 \t在org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1002)]我是缺少必須添加到我的maven pom.xml中的任何jar文件? – 2015-04-05 18:55:12

+0

對不起,但您需要發佈堆棧跟蹤以獲得進一步的幫助。至於任何其他jar,如果你還沒有,爲了JSON支持,使用[this](https://jersey.java.net/documentation/latest/media.html#json.jackson)。 – 2015-04-05 21:13:36

0

正如peeskillet所述,您的客戶端使用accept(MediaType.TEXT_PLAIN)和Accept(MediaType.TEXT_HTML)調用將導致問題,因爲您的TodoResource方法不會在@Produces註釋中指定這些媒體類型。

要麼

  1. 改變你的TodoResource類,以支持這些媒體類型
  2. 改變你的客戶端代碼刪除對應於這些媒體類型
1

檢查內容類型的來電!

設置爲.accept(MediaType.TEXT_PLAIN)爲我工作。

0

使用通俗的語言「HTTP 406不可接受」意味着您對服務器的請求不會失敗,而服務器正在說您請求的請求的內容類型不能從後端提供,或者您需要提供正確的內容鍵入標題參數。 有關標題字段的參考,您可以看到: - https://en.wikipedia.org/wiki/List_of_HTTP_header_fields

請參閱響應的服務內容類型和請求相同。

相關問題