我如何在没有任何注释的spring项目中使用Jackson Json解析? [英] How do i use Jackson Json parsing in a spring project without any annotations?

查看:78
本文介绍了我如何在没有任何注释的spring项目中使用Jackson Json解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我有一个名为Errors的枚举类,它在一个普通项目中.错误枚举包含一个int代码和一个String文本. Errors枚举用于两个不同的项目.一个项目是基于Spring的项目,另一个项目位于非Spring J2EE应用程序中.

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.

在基于Spring的项目中,Jackson是用于将Errors枚举转换为Json的库.因此,如果有请求,我们可以返回一个Errors枚举对象,它将自动转换为json表示形式,即{code:int,text:error}.Errors枚举具有批注:

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)

ErrorsSerializer类位于基于Spring的应用程序中.

The ErrorsSerializer class is located in the Spring based application.

在Errors枚举所在的普通项目中,Gson是用于将对象转换为其Json表示形式的库,我们希望删除对Jackson库的任何引用.

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.

问题

如果我从Errors枚举中删除注释,那么Spring项目将不会返回Errors枚举的正确表示,它将仅返回用引号引起来的常量.

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.

问题

我如何删除Errors枚举的注释(这将删除公共项目中的所有Jackson依赖项),但仍然让Spring项目返回正确的Json表示形式?

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?

到目前为止最好的选择

到目前为止,我想出的最好的选择是在Spring应用程序中创建一个Errors容器对象,该对象包含Errors枚举并且还具有json serialize批注.

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.

谢谢!

推荐答案

您还可以使用Module功能在基于Spring的项目中指定序列化程序. Module允许用户自定义ObjectMapper对象而无需注释.参见以下示例:

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

上面的示例打印:

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

请参阅以下链接以找到解决方案,该解决方案如何在Spring应用程序中配置ObjectMapper:

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.

这篇关于我如何在没有任何注释的spring项目中使用Jackson Json解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆