Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] and content type [application/json]

后端 未结 5 2122
青春惊慌失措
青春惊慌失措 2020-12-18 21:16

I\'m digging myself in trying to send a POST request with a JSON payload to a remote server.

This GET curl command works fine:

curl -H \"Accept:appli         


        
5条回答
  •  盖世英雄少女心
    2020-12-18 22:06

    Quite late to reply, though I've just hitten the same problem and took me some time to solve it. So, I think I'd better share it and keep track of my solution.

    Actually, the exception thrown is totally misleading. Turned out the problem is not that the MappingJackson2HttpMessageConverter didn't know how to marshall my object -which sounded strange, being JSON-, but a configuration of the underlying ObjectMapper.

    What I did is to disable the property SerializationFeature.FAIL_ON_EMPTY_BEANS like that

    restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    restTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    

    and everything started working as expected.

提交回复
热议问题