@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());
}
我的豆类:
public class RequirementBean {
private String flight_details;
//Getters and Setters
}
如果我以下面的格式发送相同的请求,我可以收到请求
{"flight_details":"Bangalore"}
但我想要的格式是上面提到的格式。
请帮我解决这个问题
试试这可能对你有用...
@RequestMapping(value="addFlightDetails", method = RequestMethod.POST)
public void addOfferTest(HttpServletRequest request, HttpServletResponse
response,@RequestBody String json){
JSONObject mainObject=new JSONObject(json);
JSONObject flightdetails=mainObject.getJSONObject("flight_details");
String flight_from=flightdetails.getString("flight_from");
String flight_from=flightdetails.getString("flight_from");
System.out.println(flight_from);
System.out.println(flight_to);
}
让我知道任何问题...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句