Bing Qiao
Bing Qiao

Reputation: 481

Spring boot "CommandLineRunner" and json

I'm writing a command line app that needs to serialize/deserialize a json file but can't seem to find a non-web example. All the code I found is to do with Restful API and Web.

So the question is how to set up serializer/deserializer and map pojos to json in a Spring boot command line app. It'd be great if there is a sample unit test that shows how to set up its Spring content. I tried the code snippet in Spring boot docs using @JsonTest Auto-configured JSON tests but it complains of not finding matching implementation.

Many thanks

Upvotes: 0

Views: 348

Answers (1)

Lukas Bradley
Lukas Bradley

Reputation: 489

Take a look at the Jackson project, specifically the ObjectMapper.

https://github.com/FasterXML/jackson

https://fasterxml.github.io/jackson-databind/javadoc/2.7/com/fasterxml/jackson/databind/ObjectMapper.html

The simplest way would be to create an ObjectMapper as a bean, and use that throughout your project.

@Configuration
public class MyConfig() {
    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper() ;
    }
}

Upvotes: 1

Related Questions