简体   繁体   English

在swagger.json中没有反映CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES

[英]CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES not reflected in swagger.json

I am using Jersey, Jax-rs and swagger (+ Spring for injection). 我正在使用Jersey,Jax-rs和swagger(注射+ Spring)。 My objects have some members and embedded objects with names that are composed of more than one word, and in code I use camelCase. 我的对象有一些成员和嵌入对象,其名称由多个单词组成,在代码中我使用camelCase。 I defined a resolver so they are emitted as underscore as per standard convention. 我定义了一个解析器,因此按照标准惯例将它们作为下划线发出。 The problem is that swagger apparently doesn't pick up this resolver, so that the objects still appear as camelCase in the swagger json. 问题是,招摇是显然没有拿起这个解析器,所以对象仍然在swagger json中显示为camelCase。

Below are some details of my specific configuration: 以下是我具体配置的一些细节:

Snippets of my pom.xml: 我的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>

I am using annotations only, so my web.xml is empty and I have an AppInitializer class. 我只使用注释,所以我的web.xml是空的,我有一个AppInitializer类。 The Swagger servlet is initialized in the class 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);
    }

}

Here is my main model: 这是我的主要模型:

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

}

The CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES in set in a ContextResolver class. 在ContextResolver类中设置CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES。 This works fine except that its not reflected in the doc generated by swagger. 这样可以正常工作,除非它没有反映在由swagger生成的doc中。

Below is the ContextResolver implementation: 以下是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;
    }
}

I suspect the problem is that the swagger servlet doesn't see the Context resolver, but I don't know how to fix this problem 我怀疑问题是swagger servlet没有看到Context解析器,但我不知道如何解决这个问题

OK thank you. 好的谢谢。 You are correct, the swagger introspector cannot follow the mapping by Jackson in this case. 你是对的,在这种情况下,摇摆不定的内省探测器不能遵循杰克逊的映射。 You can add the @ApiModelProperty(name = "property_details") annotation on each field to change the interpreted name by the swagger introspector. 您可以在每个字段上添加@ApiModelProperty(name = "property_details")注释,以通过swagger introspector更改解释名称。

Swagger doesnt use the Object Mapper you configure for the service. Swagger不使用您为服务配置的Object Mapper。 You need to configure the swagger object mapper with something like: 您需要使用以下内容配置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);
    }

When you configure the swagger, my understanding is that Swagger by default use this singleton to be used on swagger api. 当你配置swagger时,我的理解是Swagger默认使用这个单例在swagger api上使用。

Just notice that your Swagger-ui make break because the "basePath" property change as well to "lower_case_with_underscores" as notice here: https://github.com/swagger-api/swagger-core/issues/947 请注意你的Swagger-ui会因为“basePath”属性改变为“lower_case_with_underscores”而发生中断: https//github.com/swagger-api/swagger-core/issues/947

I hope this helps someone Regards 我希望这有助于某人问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM