Swagger2feature CXF xml配置

时间:2017-12-01 14:56:40

标签: java xml maven cxf swagger

我正在尝试使用Swagger配置CXF nonSpring以查看生成的json文档,但是我无法看到从Swagger生成的url的位置。 这是我的代码:

的pom.xml

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    let region = MKCoordinateRegion(center: self.mapView.userLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
    mapView.setRegion(region, animated: true)
}

的web.xml

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

带有定义和注释的主界面:

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <display-name>CXFServlet</display-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value> com.bank.AdminApplication </param-value>
    </init-param>
    <init-param>
        <param-name>jaxrs.application.address.ignore</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>jaxrs.features</param-name>
        <param-value>
                    org.apache.cxf.jaxrs.swagger.Swagger2Feature
                    (basePath=/swagger/api​docs)
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

我转到:http://localhost:8080/webapp/swagger/apidocs 但没有出现

任何建议?

1 个答案:

答案 0 :(得分:0)

好吧,最后我发现如何将CXF非弹簧与摇摆不定相结合;这是代码:

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>coop.bancocredicoop.test</groupId>
<artifactId>test-api-cxfnonspring-swagger</artifactId>
<version>1.0.0</version>
<name>test-api-cxfnonspring-swagger</name>
<description>Servicios test api rest</description>
<packaging>war</packaging>
<dependencies>
   <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>3.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
        <version>3.2.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.3</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
        </plugin>
        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
    </plugins>
</build>

的web.xml

<?xml version="1.0"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><servlet>
  <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.AdminApplication</param-value>
    </init-param>
    <init-param>
        <param-name>jaxrs.address</param-name>
        <param-value>/</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping></web-app>

AdminAplication.java

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
public class AdminApplication extends Application {
HashSet<Object> singletons = new HashSet<Object>();
public AdminApplication() {
    singletons.add(new PersonService());
}
@Override
public Set<Class<?>> getClasses() {
    HashSet<Class<?>> set = new HashSet<Class<?>>();
    return set;
}
@Override
public Set<Object> getSingletons() {
    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
    mapper.setAnnotationIntrospectors(primary, secondary);
    JacksonJsonProvider jaxbProvider = new JacksonJsonProvider();
    jaxbProvider.setMapper(mapper);
    singletons.add(jaxbProvider);
    Swagger2Feature feature = new Swagger2Feature();
    feature.setContact("Pablo Garcia");
    feature.setDescription("api Description");
    feature.setPrettyPrint(true);
    feature.setTitle("api title");
    singletons.add(feature);
    return singletons;
  }
}

PersonService.java

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/people")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "/people")
public class PersonService {

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @Path("/person/{id}/")
  @ApiOperation(
      value = "api value",
      notes = "api notes "
  )
  public Response getPerson(@PathParam("id") String personId) throws Exception {
      PersonResponse p = new PersonResponse();
      return Response.ok(p).build();
  }
}

最后,一个简单的POJO PersonResponse:

public class PersonResponse {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}