2016-11-24 131 views
0

嗨,我得到「由此請求標識的資源只能生成具有不可接受的特性的響應,根據請求」接受「標頭」錯誤,即Http 406不可接受。Springfox-Swagger2 406不可接受的錯誤-Spring 4

我使用下面的依賴關係招搖

 <dependency> 
      <groupId>io.springfox</groupId> 
      <artifactId>springfox-swagger2</artifactId> 
      <version>2.0.0</version> 
     </dependency> 

     <dependency> 
      <groupId>com.fasterxml.jackson.jaxrs</groupId> 
      <artifactId>jackson-jaxrs-json-provider</artifactId> 
      <version>2.8.4</version> 
     </dependency> 

     <dependency> 
      <groupId>com.wordnik</groupId> 
      <artifactId>swagger-annotations</artifactId> 
      <version>1.5.3-M1</version> 
     </dependency> 

我招搖的配置文件是如下

@EnableSwagger2 
@Component 
public class SwaggerConfig { 

    @Bean 
    public Docket swaggerSpringMvcPlugin() { 

     ArrayList<ResponseMessage> standardExpectedHttpResponses = 
       createStandardExpectedHttpResponses(); 

     return new Docket(DocumentationType.SWAGGER_2) 
       .useDefaultResponseMessages(false) 
       .globalResponseMessage(RequestMethod.GET, 
         standardExpectedHttpResponses) 
       .globalResponseMessage(RequestMethod.POST, 
         standardExpectedHttpResponses) 
       .globalResponseMessage(RequestMethod.PUT, 
         standardExpectedHttpResponses) 
       .globalResponseMessage(RequestMethod.DELETE, 
         standardExpectedHttpResponses) 
       /*.globalOperationParameters((List<Parameter>) new ParameterBuilder() 
         .name("xyz=id") 
         .description("Description of someGlobalParameter") 
         .modelRef(new ModelRef("string")) 
         .parameterType("query") 
         .required(true) 
         .build())*/ 
       .produces(createStandardProducesConsumes()) 
       .consumes(createStandardProducesConsumes()).pathMapping("") 
       .select() 
       .apis(RequestHandlerSelectors 
         .basePackage("com.x.y.z")) 
       .paths(PathSelectors.ant("/*")) 
       .build().apiInfo(metadata()); 
    } 

    private ApiInfo metadata() { 

     return new ApiInfoBuilder() 
       .title("API - xyz") 
       .description(
         "This is a Swagger-enabled API representation for the xyz.") 
       .version("1.0").build(); 
    } 

    private ArrayList<ResponseMessage> createStandardExpectedHttpResponses() { 
     ArrayList<ResponseMessage> messages = new ArrayList<ResponseMessage>(); 

     messages.add(new ResponseMessage(200, "Ok", null)); 
     messages.add(new ResponseMessage(400, "Bad Request", null)); 
     messages.add(new ResponseMessage(401, "Unauthorized", null)); 
     messages.add(new ResponseMessage(403, "Forbidden", null)); 
     messages.add(new ResponseMessage(404, "Not Found", null)); 
     messages.add(new ResponseMessage(500, "Internal Server Error", null)); 

     return messages; 
    } 

    private HashSet<String> createStandardProducesConsumes() { 
     HashSet<String> producesConsumes = new HashSet<String>(); 

     producesConsumes.add("application/json"); 

     return producesConsumes; 
    } 
} 

而且CORS過濾器設置的項目。有什麼遺漏?我如何解決這個問題。

當我打到服務時,它給我的迴應。它工作正常。我不必添加任何請求參數。以上鍊接給我的迴應。

當我點擊http://localhost:8080/endpoint/v2/api-docs時,出現http 406錯誤。我是否需要爲我的服務添加任何註釋?

+0

您可以發佈您CORS過濾器? 我有一個類似的問題,因爲我沒有在我的CORS過濾器中設置所有的響應頭。並嘗試升級你的swagger依賴。我使用版本2.5.0。 (我只是看到版本2.6.1發佈) – YLombardi

+0

當然..謝謝你的迴應。以下是我的CORS過濾器。 –

+0

我已經添加CORS過濾器如下 –

回答

0

我在下面的代碼中評論過我的Servlet。它開始工作。

<util:map id="mediaTypesMapping"> 
     <!-- <entry key="text/plain" value="text/plain;charset=UTF-8" /> 
     <entry key="text/html" value="text/html;charset=UTF-8" /> 
     <entry key="application/json" value="application/json;charset=UTF-8" /> 
     <entry key="application/xml" value="application/xml;charset=UTF-8" /> 
     <entry key="jsonp" value="application/javascript" /> 
     <entry key="application/javascript" value="application/javascript" /> 
     <entry key="application/x-protobuf" value="application/x-protobuf" /> --> 
     <entry key="application/json" value="application/json" /> 
    </util:map> 

我的服務URL是 http://localhost:8080/endpoint 我揚鞭的UI添加PARAM名URL。我如何改變這一點?由我昂首闊步UI製作

URL是通過查詢參數

http://localhost:8090/rendpoint?allRequestParams=q%3Did%3A後*%26FL%3Did

相關問題