Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动枚举JSON序列化程序_Java_Json_Spring Boot_Enums - Fatal编程技术网

Java Spring启动枚举JSON序列化程序

Java Spring启动枚举JSON序列化程序,java,json,spring-boot,enums,Java,Json,Spring Boot,Enums,下面是我想要转换为JSON的对象 公共类TestDto{ 私人ResponseType ResponseType; 私人长id; 私有字符串名称; } 下面的ResponseType是一个枚举 公共枚举响应类型{ TEST1(“测试消息1”)、TEST2(“测试消息2”)、TEST3(“测试消息3”); 私有字符串消息; } 下面是我要创建的JSON: {“code”:“TEST1”,“message”:“testmessage 1”,“id”:1,“name”:“name”} JSON响

下面是我想要转换为JSON的对象

公共类TestDto{
私人ResponseType ResponseType;
私人长id;
私有字符串名称;
}
下面的
ResponseType
是一个枚举

公共枚举响应类型{
TEST1(“测试消息1”)、TEST2(“测试消息2”)、TEST3(“测试消息3”);
私有字符串消息;
}
下面是我要创建的JSON:

{“code”:“TEST1”,“message”:“testmessage 1”,“id”:1,“name”:“name”}
JSON响应中的代码指向枚举的名称,JSON响应中的消息指向枚举的消息字段


有什么办法吗?

我已经更新了以前的答案,因为它不正确。您可以使用
@JsonFormat(shape=JsonFormat.shape.OBJECT)
指示枚举应该像对象一样序列化(基于getter)

之后,还必须在枚举字段上使用
@JsonUnwrapped
,以避免将其字段序列化为对象

    public static class TestDto {
       @JsonUnwrapped private ResponseType responseType;
       private Long id;
       private String name;
    }
运行以下代码

    TestDto testDto = new TestDto(ResponseType.TEST1, 1234356L, "First Response");
    result = mapper.writeValueAsString(testDto);
    System.out.println(result);

我得到的结果是
{“message”:“testmessage1”,“code”:“TEST1”,“id”:1234356,“name”:“First Response”}

最简单的方法是将派生的getter/setter添加到
TestDto
,并禁止
responseType
字段的JSON序列化

类TestDto{
私人ResponseType ResponseType;
私人长id;
私有字符串名称;
@JsonIgnore//Suppress JSON序列化
公共响应类型getResponseType(){
返回此.responseType;
}
public void setResponseType(ResponseType ResponseType){
this.responseType=responseType;
}
公共字符串getCode(){//“code”属性的派生getter
返回此.responseType.name();
}
public void setCode(字符串代码){//“code”属性的派生setter
this.responseType=(code==null?null:responseType.valueOf(code));
}
公共字符串getMessage(){//“message”属性的派生getter
返回此.responseType.getMessage();
}
@不推荐的//不应该由Java代码调用,因为它是一个伪存根方法
@抑制警告(“未使用”)
public void setMessage(字符串消息){//“message”属性的派生setter
//忽略值
}
公共长getId(){
返回此.id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回此.name;
}
公共void集合名(字符串名){
this.name=名称;
}
}
双向试验

TestDto TestDto=newtestdto();
testDto.setResponseType(ResponseType.TEST1);
testDto.setId(1L);
testDto.setName(“名称”);
ObjectMapper mapper=新的ObjectMapper();
字符串json=mapper.writeValueAsString(testDto);
System.out.println(json);
TestDto testDto2=mapper.readValue(json,TestDto.class);
System.out.println(testDto2.getResponseType());
System.out.println(testDto2.getId());
System.out.println(testDto2.getName());
输出

{“id”:1,“name”:“name”,“message”:“testmessage 1”,“code”:“TEST1”}
测试1
1.
名称

是。看看Jackson库中的JSON支持。祝你好运这不会为1个枚举输出2个属性:
response
字段->
“code”
“message”
。我误解了请求,将更新答案以匹配问题。@Slimu使用此注释获得json{“message”:“test message 1”,“id”:1234356,“name”:“First response”}如果没有枚举值的名称,是否在枚举中添加了显式getter?
    TestDto testDto = new TestDto(ResponseType.TEST1, 1234356L, "First Response");
    result = mapper.writeValueAsString(testDto);
    System.out.println(result);