Home > OS >  How can I use a JSON processor rather than jackson or gson in my spring boot application?
How can I use a JSON processor rather than jackson or gson in my spring boot application?

Time:03-27

I want to use a JSON processor like Genson in my spring boot application.

When you use Jackson or Gson you are so comfortable due to the spring boot auto-configuration for these two libraries.

In this link, there is a great guide for replacing Jackson with Gson.

For solving my problem, I created the genson and gensonBuilder bean But what should I do to be able to add this line to my config? spring.http.converters.preferred-json-mapper=genson

Do I need to do anything other than the above for replacing Jackson with Genson library?

CodePudding user response:

There is a guide for Spring Boot:
1. First of all exclude JACKSON from your Spring Boot application

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- Exclude the default Jackson dependency -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2. Register Genson converter
Genson has HTTP converter for Spring com.owlike.genson.ext.spring.GensonMessageConverter, Sources. In case Spring Boot application just create new bean with this converter. It will be applied automatically.
See Registering a custom MessageConverter in Spring Boot
Documentation

Any HttpMessageConverter bean that is present in the context will be added to the list of converters

    @Bean
    public com.owlike.genson.ext.spring.GensonMessageConverter gensonMessageConverter() {
        return new com.owlike.genson.ext.spring.GensonMessageConverter();
    }

CodePudding user response:

To use custom, not supported by default by Spring conversion library you need to implement your own AbstractJsonHttpMessageConverter:

import com.owlike.genson.Genson;
import com.owlike.genson.GensonBuilder;
import org.springframework.http.converter.json.AbstractJsonHttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;

public class GensonHttpMessageConverter extends AbstractJsonHttpMessageConverter {

    private Genson genson;

    public GensonHttpMessageConverter() {
        this(new GensonBuilder()
                .useIndentation(true)
                .create());
    }

    public GensonHttpMessageConverter(Genson genson) {
        Assert.notNull(genson, "A Genson instance is required");
        this.genson = genson;
    }

    public void setGenson(Genson genson) {
        Assert.notNull(genson, "A Genson instance is required");
        this.genson = genson;
    }

    public Genson getGenson() {
        return this.genson;
    }

    @Override
    protected Object readInternal(Type resolvedType, Reader reader) {
        return getGenson().deserializeInto(reader, resolvedType);
    }

    @Override
    protected void writeInternal(Object object, @Nullable Type type, Writer writer) {
        getGenson().serialize(object, writer);
    }
}

After that you need to register it:

import com.example.genson.GensonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new GensonHttpMessageConverter());
    }
}

See also:

  • Related