English 中文(简体)
Jackson 克服陈词滥调,有利于 came
原标题:Jackson overcoming underscores in favor of camel-case
  • 时间:2012-05-09 15:30:00
  •  标签:
  • java
  • jackson

我从互联网上取回了一名JSON星座;与大多数JSON I ve一样,我看到这包括长的钥匙,这些钥匙由强调分开。 从根本上说,我的目标是将JSON降为bject目标,但我并没有在java-code中得到强调。

例如,我可能有一个<代码>User的班级,有<代码>firstName field in camel-case,但与此同时,我需要一些办法来把Jackson到地图first_name。 JSON至firstName类别领域的关键。 是否可能?

class User{
    protected String firstName;
    protected String getFirstName(){return firstName;}
}
最佳回答

You should use the @JsonProperty on the field you want to change the default name mapping.

class User{
    @JsonProperty("first_name")
    protected String firstName;
    protected String getFirstName(){return firstName;}
}

For more info: The API

问题回答

In Jackson 2.12+, you can configure the ObjectMapper to convert camel case to names with an underscore:

objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);

或指出具有这一说明的具体模式类别:

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class;)

在Jackson 2.7之前,定名如下:

PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES

如果申请一个春季亭,申请。 财产档案,公正使用

春季,劫持者

Or Annotate the model class with this annotation.

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)

If you want this for a Single Class, you can use the PropertyNamingStrategies with the @JsonNaming, annotation, like this:

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public static class Request {

    String businessName;
    String businessLegalName;

}

Will serialize to:

{
    "business_name" : "",
    "business_legal_name" : ""
}

Jackson 2.7之前,使用PropertyNamingStrategy.LowerCaseWith Unders分

@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public static class Request {

    String businessName;
    String businessLegalName;

}

From Jackson 2.7 to Jackson 2.12 the LowerCaseWithUnderscoresStrategy is deprecated in favor of SnakeCaseStrategy, so you should use:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public static class Request {

    String businessName;
    String businessLegalName;

}

Jackson 2.12以来,PropertyNamingStrategy.SnakeCaseStrategy的改动以为准。 聚苯胺

    @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
    public static class Request {

        String businessName;
        String businessLegalName;

    }

The above answers regarding @JsonProperty and CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES are 100% accurate, although some people (like me) might be trying to do this inside a Spring MVC application with code-based configuration. Here s sample code (that I have inside Beans.java) to achieve the desired effect:

@Bean
public ObjectMapper jacksonObjectMapper() {
    return new ObjectMapper().setPropertyNamingStrategy(
            PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
}

现行最佳做法是在<条码> 申请.yml(或<条码>properties)档案中配置Jackson。

例:

spring:
  jackson:
    property-naming-strategy: SNAKE_CASE

如果你有更复杂的配置要求,你也可以从方案上召集Jackson。

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonConfiguration {

    @Bean
    public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
        return new Jackson2ObjectMapperBuilder()
                .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        // insert other configurations
    }

} 

为了说明示范类的使用:

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)

而不是:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)

自2.12起就对它进行了解释。

这里很少有答复表明两个不同版本的Jackson图书馆战略:

<>For Jackson 2.6.*

ObjectMapper objMapper = new ObjectMapper(new JsonFactory()); // or YAMLFactory()
objMapper.setNamingStrategy(
     PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

<<>For Jackson 2.7>

ObjectMapper objMapper = new ObjectMapper(new JsonFactory()); // or YAMLFactory()
objMapper.setNamingStrategy(
     PropertyNamingStrategy.SNAKE_CASE);

所有示范班级都把我视为超专业和Keneny的回答,为我打字工作:https://stackovermission.com/a/43271115/4437153'。 周期化的结果仍然是 came。

我认识到,我的春季组合存在问题,因此我不得不从另一个方面解决这一问题。 希望有人会认为它有用,但如果我不反对春天规则,请让我知道。

<>Solution for Spring MVC 5.2.5 and Jackson 2.11.2

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);           

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
    }
}
  1. 你们可以使用我们阶级领域的“JsonProperty”说明,把田地描绘成我们JSON的确切名称。

    @JsonProperty("my_name")
    private String myName;
    
  2. 您可以使用“JsonNaming”这一类别的说明,所有领域都将使用“nake”案进行脱硫。

    @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) 
    public class MyClassWithSnakeStrategy { ... 
    

}

  1. 您可以使用设计的“战略”方法,对目标申请者进行分类,以便所有序列化。

    ObjectMapper objectMapper = new ObjectMapper()
    .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);

页: 1 PropertyNamingStrategies for @JsonNaming





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签