How do i use Jackson Json parsing in a spring project without any annotations?

对着背影说爱祢 提交于 2020-01-30 06:39:45

问题


The Situation

I have an enum class called Errors and it is in a common project. The errors enum contains an int code and a String text. The Errors enum is used in two different projects. One project is a Spring based project and the other project is in a non-Spring J2EE application.

In the Spring based project, Jackson is the library used for converting the Errors enum to Json. So if there is a request we can return an Errors enum object and it will be automatically converted to the json representation, which is {code: int, text: error} The Errors enum has the annotation:

@JsonSerialize(using=ErrorsSerializer.class)

The ErrorsSerializer class is located in the Spring based application.

In the common project, where the Errors enum is located, Gson is the library used for converting objects to its Json representation and we want to remove any references to the Jackson library.

The problem

If i remove the annotation from the Errors enum the Spring project will not return the correct representation of the Errors enum, it will only return the constant variable in quotes.

The Question

How do i remove the annotation of the Errors enum (which will remove any Jackson dependencies in the common project) and yet still have the Spring project return the correct Json representation?

Best Option So Far

The best option i have come up with so far is to create an Errors container object in the Spring application that contains the Errors enum and also has the json serialize annotation.

Thanks!


回答1:


You can also specify serializer in Spring based project using Module functionality. Module allow users to customize ObjectMapper object without annotations. See below example:

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        SimpleModule module = new SimpleModule();
        module.addSerializer(Error.class, new ErrorJsonSerializer());

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);

        System.out.println(mapper.writeValueAsString(Error.NOT_FOUND));
    }
}

Above example prints:

{"code":1,"text":"Not found"}

See below links to find solution how to configure ObjectMapper in Spring app:

  • Register a custom Jackson ObjectMapper using Spring Javaconfig.
  • Configurating ObjectMapper in Spring.


来源:https://stackoverflow.com/questions/20104435/how-do-i-use-jackson-json-parsing-in-a-spring-project-without-any-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!