【问题标题】:SpringMvc RequestBody getting headersSpring Mvc 请求正文获取标头
【发布时间】:2016-11-03 18:03:17
【问题描述】:

我正在使用 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--

我试过去掉“标题”,但没有运气...... 有什么建议?谢谢

【问题讨论】:

    标签: json spring-mvc post


    【解决方案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) {
    
        ...
        ...
    }
    

    【讨论】:

    • 它给了我以下错误:方法参数类型 [java.lang.String] 缺少标头 'Content-Disposition' - 方法参数类型 [java.lang.字符串]
    • @Kunal - 请删除它并重试,Content-Disposition 是一个响应头,如果需要我们需要在发送响应时设置这个头。
    • 我设法解决了这个错误,谢谢!这是请求发送者的错误。他们遇到了一些问题,并且标题包含在正文中...谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-02-18
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2021-12-14
    • 1970-01-01
    • 2015-10-29
    相关资源
    最近更新 更多