Java 请求XML时在REST响应中显示具有多个属性的枚举
我正在使用Java和Spring5.0.1编写一个小型RESTWeb服务,但在XML响应中显示一些错误代码时遇到了问题。该类有一个“code”和“message”属性,我希望显示该属性而不是枚举值,例如“USERNAME\u EXISTS” 当用户请求Json时,我使用注释以我希望的方式显示枚举,但我似乎无法弄清楚如何让它在请求XML时以同样的方式显示。注释似乎接近我需要的,但只取一个值 有谁能给我一些建议,如何让这个显示我想要的方式?我可能还想使HttpStatus字段的行为方式相同,以便它可以显示“409”和“Conflict”,而不仅仅是“Conflict” 当我请求XML时,Spring使用它将我的对象转换为XML。我是否需要编写一些java配置和一些自定义实现来实现这个功能 如果我需要提供更多的代码示例,请告诉我。 非常感谢 这是我的错误代码类:Java 请求XML时在REST响应中显示具有多个属性的枚举,java,spring,enums,Java,Spring,Enums,我正在使用Java和Spring5.0.1编写一个小型RESTWeb服务,但在XML响应中显示一些错误代码时遇到了问题。该类有一个“code”和“message”属性,我希望显示该属性而不是枚举值,例如“USERNAME\u EXISTS” 当用户请求Json时,我使用注释以我希望的方式显示枚举,但我似乎无法弄清楚如何让它在请求XML时以同样的方式显示。注释似乎接近我需要的,但只取一个值 有谁能给我一些建议,如何让这个显示我想要的方式?我可能还想使HttpStatus字段的行为方式相同,以便它可
@XmlRootElement(name="error_code")
@XmlEnum
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum ErrorCode implements Serializable
{
USERNAME_EXISTS("1001", "The specified username already exists."),
EMAIL_IN_USE("1002", "The specified email address is already in use.");
private String code;
private String message;
private ErrorCode(String code, String message)
{
this.code = code;
this.message = message;
}
@XmlElement(name="code")
public String getCode()
{
return code;
}
@XmlElement(name="message")
public String getMessage()
{
return message;
}
}
当我请求Json时,枚举以我想要的方式显示
{
"httpStatus": "CONFLICT",
"errorMessages": [
{
"errorCode": {
"code": "1001",
"message": "The specified username already exists."
},
"developerMessage": "User could not be created due to a conflict."
}
]
}
但是请求XML不会显示枚举代码和值
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error_response>
<error_messages>
<developer_message>User could not be created due to a conflict.</developer_message>
<error_code>USERNAME_EXISTS</error_code>
</error_messages>
<http_status>CONFLICT</http_status>
</error_response>
由于冲突,无法创建用户。
用户名\u存在
冲突
JAXB只将enum
类封送为enumeration
XML类型
若要覆盖该注释,您需要使用注释提供注释。它需要将enum
类型转换为正常的类
类型,然后根据需要对其进行注释
对于您的示例,这里有一种方法可以做到这一点,将XmlAdapter
和class
类型作为enum
的静态嵌套类型,以将它们放在一起
@XmlJavaTypeAdapter(ErrorCode.Xml.Adapter.class)
enum ErrorCode {
USERNAME_EXISTS("1001", "The specified username already exists."),
EMAIL_IN_USE("1002", "The specified email address is already in use.");
private final String code;
private final String message;
private ErrorCode(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"code", "message"})
static final class Xml {
private String code;
private String message;
Xml(ErrorCode v) {
this.code = v.getCode();
this.message = v.getMessage();
}
static final class Adapter extends XmlAdapter<Xml, ErrorCode> {
@Override
public Xml marshal(ErrorCode v) throws Exception {
return new Xml(v);
}
@Override
public ErrorCode unmarshal(Xml v) throws Exception {
throw new UnsupportedOperationException();
}
}
}
}
输出
1002
指定的电子邮件地址已在使用中。
JAXB只将enum
类封送为enumeration
XML类型
若要覆盖该注释,您需要使用注释提供注释。它需要将enum
类型转换为正常的类
类型,然后根据需要对其进行注释
对于您的示例,这里有一种方法可以做到这一点,将XmlAdapter
和class
类型作为enum
的静态嵌套类型,以将它们放在一起
@XmlJavaTypeAdapter(ErrorCode.Xml.Adapter.class)
enum ErrorCode {
USERNAME_EXISTS("1001", "The specified username already exists."),
EMAIL_IN_USE("1002", "The specified email address is already in use.");
private final String code;
private final String message;
private ErrorCode(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"code", "message"})
static final class Xml {
private String code;
private String message;
Xml(ErrorCode v) {
this.code = v.getCode();
this.message = v.getMessage();
}
static final class Adapter extends XmlAdapter<Xml, ErrorCode> {
@Override
public Xml marshal(ErrorCode v) throws Exception {
return new Xml(v);
}
@Override
public ErrorCode unmarshal(Xml v) throws Exception {
throw new UnsupportedOperationException();
}
}
}
}
输出
1002
指定的电子邮件地址已在使用中。
这看起来应该能奏效!非常感谢!如果我有任何问题,我会回来的。这看起来应该可以解决问题!非常感谢!如果我有任何问题,我会回来的。