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

后端 未结 5 2121
青春惊慌失措
青春惊慌失措 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 21:47

    Spring's RestTemplate does not know how to serialize the Android org.json.JSONObject class. (org.json.JSONObject is not available in "normal"/desktop Java)

    RestTemplate for Android supports

    Jackson JSON Processor, Jackson 2.x, and Google Gson.

    per: https://docs.spring.io/autorepo/docs/spring-android/1.0.1.RELEASE/reference/html/rest-template.html

    With Jackson on the classpath RestTemplate should understand how to serialize Java beans.

    CredentialsResource cr = new CredentialsResource();
    cr.setEmail(...);
    cr.setPassword(...);
    
    HttpEntity entityCredentials = new HttpEntity(cr, httpHeaders);
    

    P.S. It would be pretty easy to write your own JSONObjectHttpMessageConverter if you wanted to carry on using JSONObject, your converter should just have to call .toString()

提交回复
热议问题