The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

wnorman (7) [Avatar] Offline
#1
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
wnorman (7) [Avatar] Offline
#2
Re: problem with REST client POST/PUT methods
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.
jmdopereiro (1) [Avatar] Offline
#3
Re: problem with REST client POST/PUT methods
Hello Bill, I faced the problem you mention here.

I´ve changed the code in order to set the header with Content-Type instead of Accept and this is what I´m getting now:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type

Any ideas ?

Thanks

Jose