首先,我使用JAXB从XSD文档创建java类,这创建了以下结构
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.bind.annotation.XmlType;
@XmlType(
name = "types"
)
@XmlEnum
public enum Types {
@XmlEnumValue("tp1")
TP1("tp1"),
@XmlEnumValue("tp2")
TP2("tp2")
private final String value;
private Types(String v) {
this.value = v;
}
public String value() {
return this.value;
}
public static Types fromValue(String v) {
Types[] arr$ = values();
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; ++i$) {
Types c = arr$[i$];
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
所以当我在SpringBoot中为我的api传递paylod时
@Override
@RequestMapping(method = RequestMethod.POST, value = "/create",
produces = "application/json",consumes = "application/json")
public Task createTask(@Valid @RequestBody Task taskRequest) throws
Exception {
return taskService.createTask(taskRequest);
}
和有效负载是
{
"type": "tp1",
"partner": {
"partnerId": 1234567,
"partnerTransactionId": "808c7c1d-e43e-4804-a192-00a61deef321"
}
}
然后我收到错误
{
"timestamp": 1514695697219,
"status": 400,
"error": "Bad Request",
"exception":
"org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not deserialize value of type
com.v1.Type from String \"tp1\": value not one of declared Enum instance
names:[TP1,TP2]; nested exception is
com.fasterxml.jackson.databind.exc.InvalidFormatException
"path": "/test/services/v1/create"
}
我尝试使用spring.jackson.deserialization.fail-on-unknown-properties=false
但它没有帮助。
如何解决这个问题?