Springfox-Swagger2 406 不可接受的错误 - Spring 4 [英] Springfox-Swagger2 406 not acceptable error -Spring 4

查看:56
本文介绍了Springfox-Swagger2 406 不可接受的错误 - Spring 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此请求标识的资源只能生成具有根据请求接受"标头不可接受的特征的响应"错误,即 Http 406 不可接受.

Hi I am getting "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers" error i.e Http 406 not acceptable.

我正在为 swagger 使用以下依赖项

I am using the below dependencies for swagger

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

我的 swagger 配置文件如下

My swagger config file is as below

@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 过滤器.缺少什么吗?我该如何解决这个问题.

Also CORS filter is setup for the project. Anything is missing ? How do i resolve this.

当我点击服务时,它给了我回应.它工作正常.我不必添加任何请求参数.上面的链接给了我回复.

When i hit the service it is giving me the response. It is working fine. I dont have to add any request parameters. Above link is giving me the response.

当我点击 http://localhost:8080/endpoint/v2/api-docs,我有 http 406 错误.我需要为我的服务添加任何注释吗?

When I hit http://localhost:8080/endpoint/v2/api-docs, I have the http 406 error. Do i need to add any annotations to my service ?

推荐答案

我在我的 Servlet 中对下面的代码进行了注释.它开始工作了.

I have commented below code in my Servlet. It started working.

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

我的服务网址是http://localhost:8080/endpoint我的 Swagger UI 正在向 URL 添加参数名称.我该如何更改?

My Service URL is http://localhost:8080/endpoint My Swagger UI is adding param name to URL. How do i change this ?

我的 swagger UI 生成的 URL 是在传递查询参数之后

URL produced by my swagger UI is after passing query param

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

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

这篇关于Springfox-Swagger2 406 不可接受的错误 - Spring 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆