@RequestBody不接受来自客户端的请求。请帮我解决这个问题
为了进行测试,我将以原始(应用程序/ JSON)的邮递员的JSON数据发送到我的控制器,格式如下
postman中的错误:客户端发送的请求在语法上是不正确的。
但我想这是JSON的正确格式。如果我错了,请纠正我
dgv_loadout.DataSource = ouput.ToList();
var activate = db.Users.ToList();
foreach (DataGridViewRow color in dgv_loadout.Rows)
{
if (activate[color.Index].Active == false)
{
dgv_loadout.DefaultCellStyle.BackColor = Color.Red;
}
}
这是我的控制器代码:
{
"flight_details": [
{
"flight_from": "Bangalore",
"flight_to": "Hyderabad"
},
{
"flight_from": "Delhi",
"flight_to": "Pune"
}]
}
My Bean课程:
@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
}
但我想要的格式是上面提到的格式。
请帮我解决这个问题
答案 0 :(得分:1)
试试这可能对你有用......
@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);
}
让我知道任何问题......
答案 1 :(得分:0)
您将json 数组发送到控制器。你需要一个列表来获得这个数组。它可能是这样的。
public class FlightDetails{
private String flight_from;
private String flight_to;
//Getters and Setters
}
public class RequirementBean {
private List<FlightDetails> flight_details;
//Getters and Setters
}
答案 2 :(得分:0)
您正在尝试将JSON数组转换为不正确的String。你可以用两种方式做到这一点 1.创建一个带有from和to字段的bean FlightDetails。在这种情况下,您的控制器变为
@RequestMapping(value="addFlightDetails", method = RequestMethod.POST)
public void addOfferTest(HttpServletRequest request, HttpServletResponse
response,@RequestBody FlightDetails[] requirementbean){ }
使用RequirementBean,其中包含FlightDetail bean的数组或列表。
public class RequirementBean { private list flight_details; // Getters和Setters }
答案 3 :(得分:0)
更改您的RequirementBean代码,如下所示
public class RequirementBean {
private List<FlightDetail> flight_details;
//Getters and Setters
}
public class FlightDetail {
private String flight_from;
private String flight_to;
//Getters and Setters
}
答案 4 :(得分:-1)
你能试试吗?
Tue, 01 Nov 1994 08:12:31 GMT