【发布时间】:2018-06-15 00:12:53
【问题描述】:
@RequestBody 不接受来自客户端的请求。请帮我解决这个问题
为了测试,我将原始(应用程序/JSON)中的 JSON 数据从邮递员发送到我的控制器,格式如下
邮递员错误:客户端发送的请求语法错误。
但我猜这是 JSON 的正确格式。如有错误请指正
{
"flight_details": [
{
"flight_from": "Bangalore",
"flight_to": "Hyderabad"
},
{
"flight_from": "Delhi",
"flight_to": "Pune"
}]
}
这是我的控制器代码:
@RequestMapping(value="addFlightDetails", method = RequestMethod.POST)
public void addOfferTest(HttpServletRequest request, HttpServletResponse
response,@RequestBody RequirementBean requirementbean){
System.out.println("flightdetails:"+requirementbean.getFlight_details());
}
我的 Bean 类:
public class RequirementBean {
private String flight_details;
//Getters and Setters
}
如果我以以下格式发送相同的请求,我能够接收请求
{"flight_details":"Bangalore"}
但我想要的格式是上面提到的那个。
请帮我解决这个问题
【问题讨论】:
-
{"flight_details":"Bangalore"}映射到String flight_details;但在您的第一个代码中,您传递了一个带有flight_from和flight_to属性的数组
标签: java json rest spring-mvc