Spring Conversion Magic - java

I am having a piece of code like below for calling one of our service.
MultiValueMap<String, String> parametersMap = new LinkedMultiValueMap<>();
parametersMap.add("query", query);
parametersMap.add("locale", "en_US");
parametersMap.add("resultsLimit", Boolean.FALSE.toString());
parametersMap.add("maxResults", maxResults);
parametersMap.add("type", "TTT");
parametersMap.add("ids", commaSeparatedValues(ids));
parametersMap.add("infoTypes", "HHH,JJJ");
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(parametersMap, getHttpHeaders());
MyEntity myEntity = restTemplate.postForEntity("http://example.com", httpEntity, MyEntity.class);
And at the server side the controller code is like
#RequestMapping("my/service")
public MyEntity suggest(#RequestParam(required = true) String query, #RequestParam(required = true) String locale,
#RequestParam(required = false) String resultsLimit, #Valid OtherOptions options)
and the OtherOption class is like
class OtherOptions {
String maxResults;
String type;
String ids;
String infoTypes;
}
Here everything is working fine, but I am confused about somethings like .
Is it a get or post request ?
How is some of the parameter maps content become request params(query params) and some others got mapped to the Object of OtherOptions ?
Which is the actual body of the request ?

Is it a get or post request ?
It is a post request. you are calling restTemplate.postForEntity. But your server side method is not restricted as you didn't specify the method attribute for RequestMapping so same method can handle any http method from the point of server.
How is some of the parameter maps content become request params(query params) and some others got mapped to the Object of OtherOptions?
None of them are query params.
See the spring docs for the meaning of #RequestParam. In your case, it all comes from body and not as query params
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#postForEntity-java.net.URI-java.lang.Object-java.lang.Class-
The body of the entity, or request itself, can be a MultiValueMap to create a multipart request.
Which is the actual body of the request?
parametersMap is the body of the http request.
Note:
Currently your call should fail because you are posting it to http://example.com at client and listening at server side on my/service

Related

RestTemplate for #RequestBody and #RequestParam

I'm trying to use RestTemplate to call POST api like that :
RequestorParam test = new RequestorParam();
test.setScopeMcg("MCG");
test.setSituatedDealIds(situatedDealIds);
String url = "http://localhost:" + serverPort + "/retrieveAttributes";
ResponseEntity<SituatedDeals> response = restTemplate.postForEntity(url, test, SituatedDeals.class);
and the code of the controller is like ;
#PostMapping(value = "/retrieveAttributes", produces = "application/json")
#ResponseBody
#Duration
public SituatedDeals retrieveAttributes(
#RequestParam String scopeMcg,
#RequestBody SituatedDealIds situatedDealIds
) {
log.info("success")
}
i'm getting bad request, can someone help ?
According to your controller code, you are actually not returning any Response Entity of type SituatedDeals, just logging it as success. this might be the reason for the null object in response.
The scopeMcg is a RequestParameter so you should be passing it in a request param format i.e http://localhost:8080/retrieveAttributes?scopeMcg=MCG
Reference:Spring Request Param
Your test Object is the actual payload for your post request which should be of type SituatedDealIds object.
Reference: Rest-Template post for Entity

API call with Java + STS returning "Content type 'application/octet-stream' not supported"

I am working on part of an API, which requires making a call to another external API to retrieve data for one of its functions. The call was returning an HTTP 500 error, with description "Content type 'application/octet-stream' not supported." The call is expected to return a type of 'application/json."
I found that this is because the response received doesn't explicitly specify a content type in its header, even though its content is formatted as JSON, so my API defaulted to assuming it was an octet stream.
The problem is, I'm not sure how to adjust for this. How would I get my API to treat the data it receives from the other API as an application/json even if the other API doesn't specify a content type? Changing the other API to include a contenttype attribute in its response is infeasible.
Code:
The API class:
#RestController
#RequestMapping(path={Constants.API_DISPATCH_PROFILE_CONTEXT_PATH},produces = {MediaType.APPLICATION_JSON_VALUE})
public class GetProfileApi {
#Autowired
private GetProfile GetProfile;
#GetMapping(path = {"/{id}"})
public Mono<GetProfileResponse> getProfile(#Valid #PathVariable String id){
return GetProfile.getDispatchProfile(id);
}
The service calling the external API:
#Autowired
private RestClient restClient;
#Value("${dispatch.api.get_profile}")
private String getDispatchProfileUrl;
#Override
public Mono<GetProfileResponse> getDispatchProfile(String id) {
return Mono.just(id)
.flatMap(aLong -> {
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return restClient.get(getDispatchProfileUrl, headers);
}).flatMap(clientResponse -> {
HttpStatus status = clientResponse.statusCode();
log.info("HTTP Status : {}", status.value());
return clientResponse.bodyToMono(GetProfileClientResponse.class);
// the code does not get past the above line before returning the error
}).map(GetProfileClientResponse -> {
log.debug("Response : {}",GetProfileClientResponse);
String id = GetProfileClientResponse.getId();
log.info("SubscriberResponse Code : {}",id);
return GetProfileResponse.builder()
// builder call to be completed later
.build();
});
}
The GET method for the RestClient:
public <T> Mono<ClientResponse> get(String baseURL, MultiValueMap<String,String> headers){
log.info("Executing REST GET method for URL : {}",baseURL);
WebClient client = WebClient.builder()
.baseUrl(baseURL)
.defaultHeaders(httpHeaders -> httpHeaders.addAll(headers))
.build();
return client.get()
.exchange();
}
One solution I had attempted was setting produces= {MediaType.APPLICATION_JSON_VALUE} in the #RequestMapping of the API to produces= {MediaType.APPLICATION_OCTET_STREAM_VALUE}, but this caused a different error, HTTP 406 Not Acceptable. I found that the server could not give the client the data in a representation that was requested, but I could not figure out how to correct it.
How would I be able to treat the response as JSON successfully even though it does not come with a content type?
Hopefully I have framed my question well enough, I've kinda been thrust into this and I'm still trying to figure out what's going on.
Are u using jackson library or jaxb library for marshalling/unmarshalling?
Try annotating Mono entity class with #XmlRootElement and see what happens.

How to consume a HTTPS GET service with Spring Boot

I am trying to consume the following HTTPS endpoints from Yahoo Weather Service:
Yahoo Weather Service API
I am doing some special query according to the API to get the current weather at some parametrized location.
#Service("weatherConditionService")
public class WeatherConditionServiceImpl implements WeatherConditionService {
private static final String URL = "http://query.yahooapis.com/v1/public/yql";
public WeatherCondition getCurrentWeatherConditionsFor(Location location) {
RestTemplate restTemplate = new RestTemplate();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(URL);
stringBuilder.append("?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22");
// TODO: Validate YQL query injection
stringBuilder.append(location.getName());
stringBuilder.append("%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
WeatherQuery weatherQuery = restTemplate.getForObject(stringBuilder.toString(), WeatherQuery.class);
// TODO: Test Json mapping response
Condition condition = weatherQuery.getQuery().getResults().getChannel().getItem().getCondition();
return new WeatherCondition(condition.getDate(), Integer.parseInt(condition.getTemp()), condition.getText());
}
Location is a class that provides the attribute "name" that is a String description of the location, such as "New York" or "Manila".
Condition an other classes just map the returning object.
When executing I get the following HTTP response:
org.springframework.web.client.HttpClientErrorException: 403 Forbidden
So this means I am not authorized to access the resource from what I understand.
The URL works great if I just copy & paste it in a web browser:
Yahoo Weather Query
I think that mapping is not a problem since I am not getting "400" (Bad Request) but "403" (Forbidden)
There must be some error on the way I use the RestTemplate object. I am researching but I can't find an answer.
The docs say you need an api key. But when I make a call like this:
fetch('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys')
.then(resp=> resp.json())
.then((res)=>console.log(res.query.results))
https://repl.it/NeoM
It works fine without one. Perhaps you've been blackisted for hitting the api too often.
Your code seems fine.
I finally found the answer. It finally WAS a Bad Request because I needed to pass the parameters differently (not as part of the URL).
I found the answer here. Here goes the code for my particular Yahoo Weather API call return a String (I still will have to do some work to use the mapping).
private static final String URL = "http://query.yahooapis.com/v1/public/yql";
public String callYahooWeatherApi() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(URL)
.queryParam("q", "select wind from weather.forecast where woeid=2460286")
.queryParam("format", "json");
HttpEntity<?> entity = new HttpEntity<>(headers);
HttpEntity<String> response = restTemplate.exchange(
builder.build().encode().toUri(),
HttpMethod.GET,
entity,
String.class);
return response.getBody();
}

How to get HttpRequest getEntity data in Spring MVC controller?

private ArrayList<NameValuePair> mParams;
HttpClient client = new DefaultHttpClient();
mParams = new ArrayList<NameValuePair>();
mParams.add(new BasicNameValuePair("testKey", "John"));
mParams.add(new BasicNameValuePair("testSerial", "003-100"));
HttpPost request = new HttpPost("http://localhost:8080/test/getRequiredEnv");
request.setEntity(new UrlEncodedFormEntity(mParams, HTTP.UTF_8));
HttpResponse response = client.execute(request);
// TestController.java
#RestController
public class TestController {
private static final Logger logger = Logger.getLogger(TestController.class);
#RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public #ResponseBody ResponseInfo getRequiredEnv(
#RequestParam("testKey") String testKey,
#RequestParam("testValue") String testValue,
#RequestHeader HttpHeaders headers) {
logger.info("Test Key [" + testKey + "]");
logger.info("Test Value [" + testValue + "]");
return new TestResponseInfo("0001", "ABC");
}
Can someone please tell me is this the correct way to get data from 'Request.setEntity' in SpringMVC rest controller or I am missing something?
Secondly, in postman "httpPost" request I pass the parameters (testKey & testValue) as headers or as body?
Thirdly, without knowing the parameters in httpPost request can I able to parse the incoming request and extract the parameters from it in Spring controller?
First of all it would be good to know the content-type of the request that is sent.
So I guess you want to get the body of the request. To get all request parameters if you don't know the parameter names beforehand you can use #RequestParam with type Map<String, String> to get all params:
#RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public #ResponseBody ResponseInfo getRequiredEnv(
#RequestParam Map<String, String> allParams,
#RequestHeader HttpHeaders headers)
But I am not sure if this works because it also depends on the content-type. E.g. for form data (application/x-www-form-urlencoded) have a look at the Spring documentation about #RequestBody which states about one of the default message converters FormHttpMessageConverter:
FormHttpMessageConverter converts form data to/from a MultiValueMap.
So try:
#RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public #ResponseBody ResponseInfo getRequiredEnv(
#RequestBody MultiValueMap<String, String>,
#RequestHeader HttpHeaders headers)
Alternatively there is also HttpServletRequest.getParameterMap() which gets you a Map. You can get the request by just including HttpServletRequest request as a method argument.
If you know the paramters beforehand, annotating your POJO that resembles the form data with #ModelAttribute should also work like so:
#RequestMapping(value = "/getRequiredEnv", method = RequestMethod.POST)
public #ResponseBody ResponseInfo getRequiredEnv(
##ModelAttribute Test myTestPojo,
#RequestHeader HttpHeaders headers)
Or you could also send data as application/json and when including jackson as a dependency, #Requestbody will map your data to a POJO. Have a look at e.g. Spring JSON request body not mapped to Java POJO.
In regard to your second question httpPost will pass the parameters as body since it is a POST request.

How to send request parameters with same parameter-name

This question is an extension to How do I set params for WS.post() in play 2.1 Java
My web service request handler is as follows
#POST
#Path("/requestPath")
public String addChallengersToLeague(
#FormParam("name") String name,
#FormParam("values") List values);
since WSRequestHolder accepts a Map<String, String> in setQueryParameter method, I am not able to send parameter list with same name.
I can send request from POSTMAN with multiple parameters having name 'values'and it works fine.
Can you suggest how to do the same using play? I am using play 2.1.3
Thanks in advance.
This can be done using play.libs.WS.WSRequest API
Following is a simple example
WSRequest request = new WSRequest("<Method>"); //Method can be GET, POST etc
request.setUrl("<service-url>");
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
com.ning.http.client.FluentStringsMap map = new com.ning.http.client.FluentStringsMap();
map.add("name", "aniket");
Set<String> values= new HashSet<String>();
values.add("1");
values.add("2");
values.add("3");
values.add("4");
map.add("values", values);
request.setQueryParameters(map);
Promise<Response> response = request.execute();
You can then use response.get().getBody() to get response body.

Categories