Passing Request Body to Spring Rest Template without using LinkedMultiValueMap - java

I'm using the Spring Rest Template to make an Http PUT request and up until now I have been passing the request body using the following:
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();
body.add("account", testAccount);
HttpEntity<?> requestEntity = new HttpEntity<Object>(body, headers);
ResponseEntity<String> responseEntity;
try {
responseEntity = rest.exchange(uri, HttpMethod.PUT, requestEntity, String.class);
}
catch (HttpStatusCodeException e) {
log.error("Http PUT failed with response: " + e.getStatusText());
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBodyAsString());
}
return responseEntity;
The request body that gets sent to my target API appears as:
{"account":[{"account_id":"495"}]}
This works, but my target API is not expecting the account object to have an array as a value and is currently giving me a 500 Internal Server Error, so, my question is, how can I get the value of the 'account' property to be an object rather than an array? For example I would like the request body to appear as:
{"account":{"account_id":"495"}}
Is there another type of Map which can be used which does not accept multiple values?
I would still like to use the exchange method if possible.
Any on help on this would be grand! Many thanks

The answer was actually simply using a regular HashMap instead of the MultiValueMap. As seen here the MultiValueMapcase is used when you want to add an array to a single key in the request body.
Thanks for all the help.

Related

Calling multiple external APIs in Spring Boot

I am working on a project but it requires me to call multiple external APIs. I basically have to call an API to get a player id by giving a name. Then use that player id to get a list of match ids. Then make calls for each match id to get details on each match. its alot and doesnt seem optimal but its the only way to do it. I was going to use rest template to make a call to the following
https://americas.api.riotgames.com/lol/match/v5/matches/by-puuid/HDzjdaStxhHcceGGd8qJcc4Vw45FOlOQ1PNXKQ0h9_iqfwHP3oI0spl1bLUOw_7_J49vzaIKylv5Vg/ids?start=0&count=20
I have to pass in headers as well such as
riot token : token
"Origin": "https://developer.riotgames.com"
I was wondering how I can do this in Java Spring boot. I saw RestTemplate would be used but I couldnt figure out how to include the headers. Any guidance would be appreciated.
You can call RestTemplate.exchange() using either the method signature with RequestEntity or with HttpEntity.
// Using RequestEntity
RequestEntity<?> request= RequestEntity.get(url).header(headerName, headerValue).build();
ResponseEntity<String> response = restTemplate.exchange(request, String.class);
// Using HttpEntity
HttpHeaders headers = new HttpHeaders();
headers.set(headerName, headerValue);
HttpEntity httpEntity = new HttpEntity(/* this is nullable */ requestBody, headers);
ResponseEntity,String> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
Would also recommend reading through the following:
https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/
https://www.baeldung.com/rest-template
How to set an "Accept:" header on Spring RestTemplate request?

Build Response Entity without sending it

I have the following situation: In my spring application I make a request to another web service. This web service sends me a ResponseEntity back. Now I have modified the ResponseEntity this worked perfectly, except that I cannot build a new ResponseEntity, I don't know why. Can someone tell me how to build a new ResponseEntity?
Thats my code:
JsonNode modifiedBody = //body
HttpHeaders modifiedHeader = //header
HttpEntity<Object> newResponse = new HttpEntity<>(modifiedBody, modifiedHeader);
//until this point everything works perfect now I want to create a new ResponseEntity without calling a webservice
ResponseEntity<Object> entity = //MAKE NEW RESPONSE ENTITY
Can someone please tell me how to make a new response entity without sending it?
Thanks in advance.
Edit:
Disappearing value:
If I look in the debugger I find the value. But in the next step its gone.
As simple as that:
ResponseEntity<Object> entity = ResponseEntity
.status(200) // status
.headers(...)
.body(...); // maybe some body
Please note that you cannot pass HttpEntity there, so you must set headers and body through these static methods as well.

Testing MockRestServiceServer spring-test with multipart request

Recently I've started to use Spring's MockRestServiceServer to verify my RestTemplate based requests in tests.
When its used for simple get/post request - all good, however, I couldn't figure out how to use it with POST multipart request:
For example, my working code that I would like to test looks like this:
public ResponseEntity<String> doSomething(String someParam, MultipartFile
file, HttpHeaders headers) { //I add headers from request
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.add("file", new ByteArrayResource(file.getBytes()) {
#Override
public String getFilename() {
return file.getOriginalFilename();
}
});
map.add("someParam", someParam);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new
HttpEntity<>(map, headers);
return this.restTemplate.exchange(
getDestinationURI(),
HttpMethod.POST,
requestEntity,
String.class);
}
So my question is How I can specify my expectations with org.springframework.test.web.client.MockRestServiceServer? Please notice, that I don't want to just mock the "exchange" method with mockito or something, but prefer to use MockRestServiceServer
I'm using spring-test-4.3.8.RELEASE version
A code snippet would be really appreciated :)
Thanks a lot in advance
Update:
As per James's request I'm adding non-working test snippet (Spock test):
MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build()
server.expect(once(), requestTo(getURI()))
.andExpect(method(HttpMethod.POST))
.andExpect(header(HttpHeaders.CONTENT_TYPE, startsWith("multipart/form-data;boundary=")))
.andExpect(content().formData(["someParam" : "SampleSomeParamValue", "file" : ???????] as MultiValueMap))
.andRespond(withSuccess("sample response", MediaType.APPLICATION_JSON))
multipartFile.getBytes() >> "samplefile".getBytes()
multipartFile.getOriginalFilename() >> "sample.txt"
I get exception while asserting the request content. The form data is different, because an actual form data is created internally with Content-Disposition, Content-Type, Content-Length per parameter and I don't know how to specify these expected values
Multipart request expectations have been added to MockRestServiceServer in Spring 5.3 - see:
pull request
final version
You can use
content().multipartData(MultiValueMap<String, ?> expectedMap)
Parse the body as multipart data and assert it contains exactly the values from the given MultiValueMap. Values may be of type:
String - form field
Resource - content from a file
byte[] - other raw content
content().multipartDataContains(Map<String,?> expectedMap)
Variant of multipartData(MultiValueMap) that does the same but only for a subset of the actual values.
I think this depends on how deeply you want to test the form data. One way, which is not 100% complete, but is a "good enough" for unit testing (usually) is to do something like:
server.expect(once(), requestTo(getURI()))
.andExpect(method(HttpMethod.POST))
.andExpect(content().string(StringContains.containsString('paramname=Value') ))....
This is ugly and incomplete, but is sometimes useful. Of course, you can also work to make the form setup it's own method and then use mocks to try to verify that the expected parameters are all in place.

RestTemplate not working with APPLICATION_FORM_URLENCODED and Request Pojo

I am using restTemplate to consume a service.
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity request = new HttpEntity(countryRequest, headers);
CountryResponse response = restTemplate.postForObject(countryURL, request, CountryResponse.class);
countryRequest is a object of a POJO with just a string field code.
restTemplate has jackson2HttpMessageConverter and FormHttpMessageConverter in messageConverters.
I am getting the following exception :
org.springframework.web.client.RestClientException:
Could not write request: no suitable HttpMessageConverter found for request type [CountryRequest] and content type [application/x-www-form-urlencoded]
But if I use MultiValueMap instead of CountryRequest, I got the 200 response:
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add(code, "usa");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(map, headers);
Is there any way to replace the MultiValueMap approach here?
There is two main kinds of request serialization: as usual FORM data or as JSON object.
Form Data is simplest and oldest way which sends a simple key-value pairs of strings in POST payload. But when you need to send some object with nested properties or some list or even map then it becomes a problem.
That's why everybody tries to use a JSON format which can be more easily deserialized into POJO object. And this is a de facto standard for modern web.
So in your case for some reason RestTemplate tries to serialize the CountryRequest but it don't know how to serialize it into FORM data.
Try to replace the request with a pojo that you are sending:
CountryRequest request = new CountryRequest();
CountryResponse response = restTemplate.postForObject(countryURL, request, CountryResponse.class);
Then RestTemplate tries to serialize the CountryRequest into JSON (which is default behavior).

How to properly setup a POST spring rest template by passing in parameters and return json?

I am a bit new to spring and I am having issues calling a rest service using post to a ur in spring by using spring templates
I am not sure how to properly pass in data into the rest template and how to get json data out currently i get 415 unsupport media type error.
So the RestservicesURL.signupurl = "abc.com/signup
the param would be = "name=john?email=john#doe.com?password=john" (which is acquired by #RequestParam Map by spring)
The response I need to get is a JSON object and I am not sure how to do this;
public void signUp(Map<String, String> param) {
try {
callService(RestServicesUrl.SIGNUP_URL, param);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Here is the rest template call
public static void callService(String url, Map<String, String> data) throws Exception {
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.ALL);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<String> entity = new HttpEntity<String>(headers);
RestTemplate rest = new RestTemplate();
rest.postForObject(url, entity, byte[].class);
}
Are you trying to post JSON as well as receive it?
Based on your "data" variable it looks like you might be trying to send JSON. I see you have "ALL" set on the acceptable media types, if you're posting JSON and expect to receive it back, you should put the acceptable media type and Content-Type headers to "application/json". I believe Spring has constants for these. This could be the cause of your 415 unsupported mediatype error.
As for creating your JSON, you should strongly consider a serialization framework to convert your Java objects into JSON for you and vice versa - Jackson is a very popular option and is tied closely into Spring's functionality for this, see details here: RestTemplate + Jackson
If that's not an option, you'll need to figure out a way to get your data object converted into the appropriate HTTP request with an HTTPMessageConverter.

Categories

Resources