【问题标题】:@RequestBody in Spring Restful Web API not accepting request from client where request is JSONSpring Restful Web API中的@RequestBody不接受来自请求为JSON的客户端的请求
【发布时间】: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


【解决方案1】:

你可以试试这个吗?

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

【讨论】:

    【解决方案2】:

    您将 json array 发送到 controller。你需要一个列表来获取这个数组。可能是这样的。

     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
    }
    

    【讨论】:

      【解决方案3】:

      您正在尝试将 JSON 数组转换为不正确的字符串。你可以通过两种方式做到这一点 1. 创建一个包含 from 和 to 字段的 bean FlightDetails。在这种情况下,您的控制器变为

      @RequestMapping(value="addFlightDetails", method = RequestMethod.POST)
      
      public void addOfferTest(HttpServletRequest request, HttpServletResponse 
      response,@RequestBody FlightDetails[] requirementbean){ }
      
      1. 使用包含 FlightDetail bean 的数组或列表的 RequirementBean。

        公共类RequirementBean { 私人名单 flight_details; //Getter 和 Setter }

      【讨论】:

        【解决方案4】:

        如下更改您的 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  
        }
        

        【讨论】:

        • 已经尝试过这种方式,但对我不起作用。谢谢@Gnanasekaran
        【解决方案5】:

        试试这可能对你有用...

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

        如有任何问题,请告诉我...

        【讨论】:

          猜你喜欢
          • 2020-12-21
          • 1970-01-01
          • 2013-12-30
          • 2012-04-18
          • 2020-07-18
          • 2015-04-02
          • 1970-01-01
          • 1970-01-01
          • 2014-02-03
          相关资源
          最近更新 更多