[英]Enums as Request Parameters in Spring Boot Rest
I am new to spring boot and trying to use Enum as a parameter of a rest request. 我是Spring的新手,并尝试使用Enum作为休息请求的参数。
This is my Enum class: 这是我的Enum课程:
public enum Month {
JANUARY (1, "january"), FEBRUARY(2,"february"), MARCH(3,"march"),
APRIL(4,"april"), MAY(5,"may"), JUNE(6,"june"), JULY(7,"july"),
AUGUST(8, "august"), SEPTEMBER(9,"september"), OCTOBER(10,"october"),
NOVEMBER(11,"november"), DECEMBER(12,"december");
private String desc;
private int id;
//Constructure
//Getters and Setters
}
In my controller class I am using this method: 在我的控制器类中,我使用此方法:
@RequestMapping(value = "/testmonth", method = RequestMethod.POST)
public Month TestForMonth(@RequestBody Month inputPayload) {
Month response = inputPayload;
response.setId(inputPayload.getId());
response.setDesc(inputPayload.getDesc());
System.out.println("As String: " + inputPayload.getDesc() + ". As int " + inputPayload.getId() + ".");
return response;
}
This is my JSON: 这是我的JSON:
{
Month: "JANUARY"
}
But it is not working.. I am getting this error: 但它不起作用..我收到此错误:
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.example.simplerestapis.models.Month` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.example.simplerestapis.models.Month` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]]
Your body is declared as simple enum type, not object. 您的身体被声明为简单的枚举类型,而不是对象。 So instead of posting JSON object with braces try to post only a value, which is for example:
因此,不要使用大括号发布JSON对象,而是尝试仅发布一个值,例如:
"JANUARY"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.