![](/img/trans.png)
[英]Spring Boot Rest Web Service fetching multiple parameters in Get Request
[英]Enums as Request Parameters in Spring Boot Rest
我是Spring的新手,並嘗試使用Enum作為休息請求的參數。
這是我的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
}
在我的控制器類中,我使用此方法:
@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;
}
這是我的JSON:
{
Month: "JANUARY"
}
但它不起作用..我收到此錯誤:
.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]]
您的身體被聲明為簡單的枚舉類型,而不是對象。 因此,不要使用大括號發布JSON對象,而是嘗試僅發布一個值,例如:
"JANUARY"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.