2016-11-24 327 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