|
Replies:
1
-
Pages:
1
-
Last Post:
Nov 2, 2011 5:57 PM
by: wnorman
|
|
|
Posts:
3
From:
Boston, MA
Registered:
10/27/11
|
|
|
|
problem with REST client POST/PUT methods
Posted:
Oct 27, 2011 8:43 AM
|
|
Hello,
I am working through ch. 11 trying to get the REST client to perform a POST/PUT. I am having no luck with this and wonder if anyone might have some insight as to what is wrong.
For example: my client creates a new Spitter instance and populates it like this: Spitter spitter = new Spitter(); spitter.setUsername("cwagon"); spitter.setFullName("Chuck Wagon"); spitter.setEmail("cwagon@habuma.com"); spitter.setPassword("letmein01"); spitter.setUpdateByEmail(false);
Then makes a call into a function which is defined as: (method is redefined as static so I can call this from main())
public static Spitter newSpitter(Spitter spitter) { RestTemplate rest = new RestTemplate(); MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add("Accept", "application/json"); HttpEntity<Object> entity = new HttpEntity<Object>(spitter, headers); ResponseEntity<Spitter> response = rest.exchange( "http://localhost:8084/spitter-web/spitters", HttpMethod.POST, entity, Spitter.class);
return response.getBody(); }
The server responds to this call with: [ WARN] 08:31:13 RestTemplate - POST request for "http://localhost:8084/spitter-web/spitters" resulted in 500 (Internal Server Error); invoking error handler
Digging into this I find that it is the result of trying to enter a null value for Spitter.username in the RDB.
Sure enough, when I set a break point on the Controller method I see that the spitter instance passed by the client contains all null fields. This is what puzzles me. Various tests I have made using the GET method have worked fine. But whenever I attempt a PUT or POST the Controller sees a null valued spitter instance.
I figured the message converters may not be loaded in the client but I looped over these through the RestTemplate..getMessageConverters() and found the following converters automatically loaded:
org.springframework.http.converter.ByteArrayHttpMessageConverter@7054f9f1 org.springframework.http.converter.StringHttpMessageConverter@5ae576d4 org.springframework.http.converter.ResourceHttpMessageConverter@edc86eb org.springframework.http.converter.xml.SourceHttpMessageConverter@301db5ec org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter@6e92b1a1 org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@60cbf9bd org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@6f7918f0
Also it appears to me that the converters must be doing their job because I can make GET calls with JSON and xml.
So this is the question: Why does the server Controller not see the spitter instance that my client passes?
Thanks for your help.
-Bill
|
|
Posts:
3
From:
Boston, MA
Registered:
10/27/11
|
|
|
|
Re: problem with REST client POST/PUT methods
Posted:
Nov 2, 2011 5:57 PM
in response to:
wnorman
|
|
I found out what the issue was. My client call was actually being handled by the SpitterController.addSpitterFromForm() method. The correct controller method should be createSpitter(). The reason the call was not taking this path was because I set client call header as:
headers.add("Accept", "application/json");
But the RequestMapping for createSpitter() specifies headers = "Content-Type=application/json"
So alter the client call as:
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add("Content-Type", "application/json"); //Note Content-Type as opposed to Accept HttpEntity<Object> entity = new HttpEntity<Object>(spitter, headers); ResponseEntity<Spitter> response = rest.exchange( "http://localhost:8084/spitter-web/spitters", HttpMethod.POST, entity, Spitter.class);
and it's all good.
|
|
|
Legend
|
|
Gold: 300
+
pts
|
|
Silver: 100
- 299
pts
|
|
Bronze: 25
- 99
pts
|
|
Manning Author
|
|
Manning Staff
|
|