I need to make a request to an HTTP endpoint having a query parameter represented as JSON using Spring RestTemplate.
restTemplate.getForObject(
apiRoot + "/path" + "?object={myObject}",
Response.class,
new MyObject())
Here I need MyObject to be converted to JSON (and URL-encoded obviously). But RestTemplate just converts it to String with toString call instead. MyObject is convertable to JSON by Jackson. UriComponentsBuilder behaves the same way:
UriComponentsBuilder.fromHttpUrl(apiRoot)
.path("/path")
.queryParam("object", new MyObject()))
.queryParam("access_token", accessToken)
.toUri()
Is there a way to avoid calling ObjectMapper.writeValueAsString by hands?
Update: to clarify, in the result I need to have ?object={"key":42} in my URI (or in URL-encodeded form ?object=%7B%22key%22%3A42%7D) given MyObject has one property key with value equal to 42.
ObjectMapper.writeValueAsStringinMyObject.toString()if that's appropriate.ObjectMappervia context in a real project. And second, pretty vague one, I would rather not to introduce alternative path for conversion of objects to JSON and stay explicit by callingObjectMapperdirectly when I need to.