Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - Spring boot Jersey Jackson

I have a question related to the Jackson configuration on my Spring boot project

As described on spring boot blog

I try to customize my Object serialization.

After added a new config bean in my config

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();

    builder.propertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    return builder;
}

When I try to output an instance of my class User the json result is not in CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES

Class User {
    private String firstName = "Joe Blow";

    public String getFirstName() {
        return firstName;
    }
}

json output is :

{
  "firstName": "Joe Blow"
}

and not

{
  "first_name": "Joe Blow"
}

Maybe I need to register something in my Jersey config to activate my custom obejctMapper Config

@Configuration
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        packages("my.package);
    }
}

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The general way to configure the ObjectMapper for JAX-RS/Jersey applications is use a ContextResolver. For example

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

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

It should be picked up with the package scan, or you can explicitly register it, if it's not within the package scope

public JerseyConfig() {
    register(new ObjectMapperContextResolver());
    // Or if there's is an injection required
    // register it as a .class instead of instance
}

The ContextResolver is called during the marshalling and unmarshalling. The class/type being serialzed or deserialized into will be passed to the getContext method. So you could even use more than one mapper for different types, or even more use cases.


UPDATE

Starting from Spring Boot 1.4, you can just create an ObjectMapper Spring bean, and Spring Boot will create the ContextResolver for you, and use your ObjectMapper

// in your `@Configuration` file.
@Bean
public ObjectMapper mapper() {}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...