Http Post with request content type form not working in Spring MVC 3

后端 未结 7 1575
旧时难觅i
旧时难觅i 2020-12-02 11:13

code snippet:

@RequestMapping(method = RequestMethod.POST)//,  headers = \"content-type=application/x-www-form-urlencoded\")
public ModelAndView create(@Req         


        
7条回答
  •  自闭症患者
    2020-12-02 11:59

    Below worked for me

    On server side:

     @RequestMapping(value = "test", method = RequestMethod.POST, consumes = {"application/xml", "application/json"})
            @ResponseStatus(HttpStatus.OK)
            public @ResponseBody
            String methodName(@RequestBody EntityClassName entity) {
    

    On client side:

    String json = new JSONStringer().object()
                            .key("key").value("value")
                            .endObject()
                            .toString();
    StringEntity se = new StringEntity(json);
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    request.setEntity(se);
    HttpResponse response = client.execute(request);
    

提交回复
热议问题