简体   繁体   English

Spring Restful Web API中的@RequestBody不接受来自请求为JSON的客户端的请求

[英]@RequestBody in Spring Restful Web API not accepting request from client where request is JSON

@RequestBody not accepting the request from the client. @RequestBody不接受来自客户端的请求。 Kindly help me to solve this 请帮助我解决这个问题

For testing, I am sending JSON data from postman in raw(application/JSON) to my controller in the below format 为了进行测试,我以以下格式将原始(应用程序/ JSON)中的邮递员的JSON数据发送至我的控制器

Error in postman: The request sent by the client was syntactically incorrect. 邮递员中的错误:客户端发送的请求在语法上不正确。

But I guess this is the correct format of JSON. 但是我想这是JSON的正确格式。 Please correct me if am wrong 如果有错请指正

{
"flight_details": [
{
  "flight_from": "Bangalore",
  "flight_to": "Hyderabad"
},
{
  "flight_from": "Delhi",
  "flight_to": "Pune"
}]
}

Here is my controller code: 这是我的控制器代码:

@RequestMapping(value="addFlightDetails", method = RequestMethod.POST)

public void addOfferTest(HttpServletRequest request, HttpServletResponse 
response,@RequestBody RequirementBean requirementbean){

    System.out.println("flightdetails:"+requirementbean.getFlight_details());

}

My Bean class: 我的Bean类:

public class RequirementBean {

  private String flight_details;
 //Getters and Setters
}

If I am sending the same request in the below format I am able to receive request 如果我以以下格式发送相同的请求,则可以接收请求

{"flight_details":"Bangalore"}

But my desired format is the one which mentioned above. 但是我想要的格式是上面提到的格式。

Please help me to fix this issue 请帮助我解决此问题

Try this might works for you... 试试这个可能对你有用...

@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);

 }

let me know any issues... 让我知道任何问题...

You send a json array to controller . 您将json 数组发送到controller you need to a list to get this array. 您需要一个列表来获取此数组。 It could be like this. 可能是这样。

 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
}

You are trying to convert a JSON array to String which is incorrect. 您正在尝试将JSON数组转换为不正确的字符串。 You can do it in two ways 1. Create a bean FlightDetails with fields from and to. 您可以通过两种方式做到这一点:1.创建一个包含字段from和to的bean FlightDetails。 In this case your controller becomes 在这种情况下,您的控制器将变为

@RequestMapping(value="addFlightDetails", method = RequestMethod.POST)

public void addOfferTest(HttpServletRequest request, HttpServletResponse 
response,@RequestBody FlightDetails[] requirementbean){ }
  1. Use RequirementBean which contains array or list of FlightDetail beans. 使用RequirementBean,其中包含FlightDetail bean的数组或列表。

    public class RequirementBean { private List flight_details; 公共类RequirementBean {私有列表flight_details; //Getters and Setters } // Getters and Setter}

Change your RequirementBean code as below 如下更改您的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  
}

Can you try this ? 你可以试试这个吗?

@RequestMapping(value="addFlightDetails",method=RequestMethod.POST)
    public @ResponseBody void addFlightDetails(@RequestBody String data,HttpServletRequest request,HttpServletResponse response,HttpSession session,OutputStream outp) {


        DBObject  dBObject = new BasicDBObject();
        String message = "";        
            JSONParser parser = new JSONParser();
            Object obj = parser.parse(new StringReader(data));
            JSONObject jsonObject = (JSONObject) obj;

            System.out.println(jsonObject);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM