1111
1212import org .springframework .core .ParameterizedTypeReference ;
1313import org .springframework .http .HttpEntity ;
14+ import org .springframework .http .HttpHeaders ;
1415import org .springframework .http .HttpMethod ;
16+ import org .springframework .http .MediaType ;
1517import org .springframework .http .ResponseEntity ;
1618import org .springframework .web .client .RestTemplate ;
1719
@@ -50,15 +52,24 @@ protected void delete(String uri) {
5052 }
5153
5254 protected <T , S > S create (String uri , T body , Class <S > responseType ) {
53- ResponseEntity <S > exchange = restTemplate .exchange (uri , HttpMethod .POST , new HttpEntity <T >(body ), responseType );
55+ HttpEntity <T > requestEntity = gettHttpEntity (body );
56+ ResponseEntity <S > exchange = restTemplate .exchange (uri , HttpMethod .POST , requestEntity , responseType );
5457 return exchange .getBody ();
5558 }
5659
5760 protected <T > void update (String uri , T body ) {
58- restTemplate .exchange (uri , HttpMethod .PUT , new HttpEntity <T >(body ), Void .class );
61+ HttpEntity <T > requestEntity = gettHttpEntity (body );
62+ restTemplate .exchange (uri , HttpMethod .PUT , requestEntity , Void .class );
5963 }
60-
64+
6165 protected <T > void edit (String uri , T body ) {
62- restTemplate .exchange (uri , HttpMethod .PATCH , new HttpEntity <T >(body ), Void .class );
66+ HttpEntity <T > requestEntity = gettHttpEntity (body );
67+ restTemplate .exchange (uri , HttpMethod .PATCH , requestEntity , Void .class );
68+ }
69+
70+ private static <T > HttpEntity <T > gettHttpEntity (T body ) {
71+ HttpHeaders jsonHeaders = new HttpHeaders ();
72+ jsonHeaders .setContentType (MediaType .APPLICATION_JSON );
73+ return new HttpEntity <>(body , jsonHeaders );
6374 }
6475}
0 commit comments