CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES未反映在swagger.json

时间:2015-12-31 08:41:20

标签: java swagger jersey-2.0

我正在使用Jersey,Jax-rs和swagger(+注射用Spring)。我的对象有一些成员和嵌入对象,其名称由多个单词组成,在代码中我使用camelCase。 我定义了一个解析器,因此按照标准惯例将它们作为下划线发出。问题是,招摇是显然没有拿起这个解析器,因此对象仍然在swagger json中显示为camelCase。

以下是我具体配置的一些细节:

我的pom.xml的片段:

<properties>
        <springframework.version>4.2.3.RELEASE</springframework.version>
        <glassfish.jersey.version>2.22.1</glassfish.jersey.version>
    </properties>
<dependencies>

...

<!-- Jersey -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${glassfish.jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${glassfish.jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${glassfish.jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${glassfish.jersey.version}</version>
        </dependency>

<!-- swagger documentation -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.0</version>
        </dependency>

.....

</dependencies>

我只使用注释,所以我的web.xml是空的,我有一个AppInitializer类。 Swagger servlet在类

中初始化
public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MongoDbConfig.class);
        ctx.register(CORSResponseFilter.class);
        ctx.setServletContext(container);

        container.addListener(new ContextLoaderListener(ctx));
        container.addListener(new RequestContextListener());

        ServletRegistration.Dynamic servlet = container.addServlet("jersey-servlet",
                new org.glassfish.jersey.servlet.ServletContainer());
        servlet.setInitParameter("jersey.config.server.provider.packages", "io.swagger.jaxrs.listing,com.my.server.config,com.my.server.resource");
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/api/*");

        ServletRegistration.Dynamic swagger = container.addServlet("SwaggerServlet",
                new io.swagger.jersey.config.JerseyJaxrsConfig());
        swagger.setInitParameter("api.version", "1.0.0");
        swagger.setInitParameter("swagger.api.basepath", container.getContextPath()+"/api");
        swagger.setLoadOnStartup(2);
    }

}

这是我的主要模特:

import java.io.Serializable;

import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;

import io.swagger.annotations.ApiModel;


/**
 * @author Tamar Rosen
 *
 */


@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel
public class Property implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;    

    private String id;

    @NotNull
    private String name;

    @NotNull
    private String description;

    private PropertyDetails propertyDetails;

    private int monthlyTax;

    private String schemaUrl;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public PropertyDetails getPropertyDetails() {
        return propertyDetails;
    }

    public void setPropertyDetails(PropertyDetails propertyDetails) {
        this.propertyDetails = propertyDetails;
    }

    public int getMonthlyTax() {
        return monthlyTax;
    }

    public void setMonthlyTax(int monthlyTax) {
        this.monthlyTax = monthlyTax;
    }

    public String getSchemaUrl() {
        return schemaUrl;
    }

    public void setSchemaUrl(String schemaUrl) {
        this.schemaUrl = schemaUrl;
    }

}

在ContextResolver类中设置CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES。这样可以正常工作,除非它没有反映在由swagger生成的doc中。

以下是ContextResolver实现:

    import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper;

    public ObjectMapperContextResolver() {
        mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(
            PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES
        );
    }

    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }
}

我怀疑问题是swagger servlet没有看到Context解析器,但我不知道如何解决这个问题

2 个答案:

答案 0 :(得分:0)

好的,谢谢。你是对的,在这种情况下,摇摆不定的内省探测器不能遵循杰克逊的映射。您可以在每个字段上添加@ApiModelProperty(name = "property_details")注释,以通过swagger introspector更改解释名称。

答案 1 :(得分:0)

Swagger不使用您为服务配置的Object Mapper。 您需要使用以下内容配置swagger对象映射器:

...
ApplicationConfig.initializeSwaggerConfiguration(); // call from app config
...

/* Swagger configuratoin */
    @PostConstruct
    public static void initializeSwaggerConfiguration() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setSchemes(new String[] { "http" });

        String host = PropertyHandler.getInstance().getValue(PARAM_API_HOST) + ":"
                + PropertyHandler.getInstance().getValue(PARAM_PORT);
        beanConfig.setHost(host);
        beanConfig.setBasePath(PropertyHandler.getInstance().getValue(PARAM_API_BASEPATH));
        beanConfig.setVersion(PropertyHandler.getInstance().getValue(PARAM_VERSION) + " Profile: " + App.getProfile());
        beanConfig.setTitle("SOD Services");
        beanConfig.setDescription("Services for handlign the be");
        beanConfig.setResourcePackage(SERVICES_PACKAGE);
        beanConfig.setScan(true);
        Json.mapper().setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    }

当你配置swagger时,我的理解是Swagger默认使用这个单例在swagger api上使用。

请注意你的Swagger-ui是因为&#34; basePath&#34;财产变化以及&#34; lower_case_with_underscores&#34;如上所述:https://github.com/swagger-api/swagger-core/issues/947

我希望这有助于某人 此致