使用Swagger 2.0从API规范生成的Apache CXF Jax-rs服务器,我需要添加什么才能使其正常工作? [英] Apache CXF Jax-rs server generated from API spec using Swagger 2.0, what do I need to add to get this working?

查看:170
本文介绍了使用Swagger 2.0从API规范生成的Apache CXF Jax-rs服务器,我需要添加什么才能使其正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Swagger 2.0规范生成了Apache CXF JAX-RS REST api服务器端代码,从而通过CXF 3.1.8版依赖项创建了该规范。我想了解下一步如何运行REST应用程序服务器(独立Jetty或Spring Boot或Tomcat)。我专门在寻找涉及的Maven依赖项+我需要添加的配置。我发现很难简短地从可用文档中收集正确的详细信息。

I have generated Apache CXF JAX-RS REST api server side code using Swagger 2.0 specification thus created with CXF version 3.1.8 dependencies. I want to understand what is the next step in order to get a REST application server running(standalone Jetty or Spring Boot or Tomcat). I am specifically looking for maven dependencies involved + configurations I need to add. I found it difficult to gather the correct details from available documentation as is it brief.

我尝试遵循CXF示例,但遇到了以下问题:

I tried following the CXF samples but keep running into issues for eg:

Mar 07, 2017 4:57:53 PM org.apache.cxf.jaxrs.utils.JAXRSUtils readFromMessageBody
WARNING: No message body reader has been found for request class ABC, ContentType : application/json.
Mar 07, 2017 4:57:53 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: WebApplicationException has been caught : no cause is available

由于Swagger的参与,我发现很难理解依赖性。示例代码可以正常工作,但不能与Swagger生成的代码一起使用,当我使用它时,它会出现上述异常。

I am finding it difficult to understand the dependencies because of involvement of Swagger. The sample code works fine but not with the one generated with Swagger, when I use it, it gives above exception.

我想了解下一步,如果能

I want to understand the next steps and would appreciate if anyone who has done it with Swagger can help.

谢谢!

推荐答案

未找到邮件正文阅读器通常在您尚未注册提供程序时发生。

No message body reader has been found Generally occurs when you have not registered providers.

@Bean
public JacksonJsonProvider jacksonJsonProvider(){
    return new JacksonJsonProvider();
}

这里是使用Swagger2的CXF完整的Spring引导配置。

Here is complete Spring boot configuration for CXF with Swagger2.

使用CXF相关依赖项更新了Pom.xml

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.1.10</version>
        </dependency>

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

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.1.1-1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-service-description</artifactId>
            <version>3.1.10</version>
        </dependency>

        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>angular-swagger-ui</artifactId>
            <version>0.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7</version>
        </dependency>

        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>bootstrap-less-only</artifactId>
            <version>3.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
            <version>3.1.10</version>
        </dependency>

注意:由于我使用的是angular-swagger-ui,因此增加了其他依赖性

配置CXF Bean。

@Configuration
public class CxfConfig {

    @Bean
    public Swagger2Feature swagger2Feature(){
        Swagger2Feature feature = new Swagger2Feature();
        feature.setPrettyPrint(true);
        feature.setUsePathBasedConfig(true);
        feature.setScan(false);
        feature.setBasePath("/swagger");
        return feature;
    }

    @Bean
    public JacksonJsonProvider jacksonJsonProvider(){
        return new JacksonJsonProvider();
    }

}

启用cxf组件自动在application.properties中扫描

cxf.jaxrs.component-scan=true

创建HTML文件apidoc.html

Create html file apidoc.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
    href="/webjars/angular-swagger-ui/dist/css/swagger-ui.min.css">
<script src="/webjars/angular/angular.min.js"></script>
<script src="/webjars/angular-swagger-ui/dist/scripts/swagger-ui.min.js"></script>
<script src="/webjars/angular-sanitize/angular-sanitize.min.js"></script>
<script src="/webjars/jquery/jquery.min.js"></script>
<script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
<script src="js/cxf.js"></script>
</head>
<body ng-app="cxfApp" ng-controller="cxfCtrl" >
    <div swagger-ui url="cxfUrl" api-explorer="true" validator-url="false"></div>

</body>
</html>

在cxf.js中启用angular-swggaer

angular.module('cxfApp', ['ngSanitize', 'swaggerUi']).controller("cxfCtrl", function($scope) {

    $scope.cxfUrl = '/services/swagger.json';

})

现在开始启动spring-boot应用程序。验证以下内容。

Now on starting spring-boot application. Verify the following.


http:// localhost:8080 / services?_wadl

用于WADL文件

http:// localhost:8080 / services / swagger.json

用于Swagger JSON文件

http:// localhost:8080 / .html

对于angular-swagger-ui

http://localhost:8080/services?_wadl
For WADL file
http://localhost:8080/services/swagger.json
For Swagger JSON File
http://localhost:8080/.html
For angular-swagger-ui

这篇关于使用Swagger 2.0从API规范生成的Apache CXF Jax-rs服务器,我需要添加什么才能使其正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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