SpringMvc RequestBody获取标题

时间:2016-07-01 10:48:29

标签: json spring-mvc post

我使用的是SpringMVC,这是我的POST方法:

RequestMapping(value = "/test/handler", 
            method = RequestMethod.POST,
        headers = {"Content-type=application/json"})
    public String getOpen(@RequestBody String json, HttpServletRequest request) throws InterruptedException {

        String result= json;                        

        ObjectMapper mapper = new ObjectMapper();
        DataJsonMailJet jsonobj = null;
        try {

            // read from file, convert it to jsonobj class
            jsonobj = mapper.readValue(json, DataJsonMailJet.class);

            // display to console
            System.out.println(jsonobj);

        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }


    return (result);

    }

问题是我的“json”对象甚至得到了这样的标题:

------------------------------2fe344dd21f3
Content-Disposition: form-data; name="0"

[{"event":"open","time":1467369924, .......}]
------------------------------2fe344dd21f3--

我试过取出“标题”但没有运气...... 有什么建议?感谢

1 个答案:

答案 0 :(得分:1)

试试这个

@RequestMapping(value = "/test/handler", method = RequestMethod.POST)
public String getOpen(
        @RequestHeader(value="Accept") String accept,
        @RequestHeader(value="Content-Type") String contentType,
        @RequestBody String json,
        HttpServletResponse response) {

    ...
    ...
}