Spring Framework & RestTemplate: Cannot use REST service
First of all, I have two Spring REST services: Service A and Service Bed and . A service will have to use some of the methods available service Bed and . And A and B , Spring @RestController
.
Service A has a POST method:
@RequestMapping(value = "/mediumcandy/linkreachable", method = RequestMethod.POST)
public ResponseEntity<ShortURL> shortenerIfReachable(@RequestParam("url") String url,
@RequestParam(value = "sponsor", required = false) String sponsor,
@RequestParam(value = "brand", required = false) String brand,
HttpServletRequest request) {
ShortURL su = null;
/*************************************************************
* CONSUMING REST Service
*************************************************************/
Map<String, String> vars = new HashMap<String, String>();
vars.put("url", url);
RestTemplate restTemplate = new RestTemplate();
su = restTemplate.postForObject("http://SERVICE_URI_HERE/linkreachable", null, ShortURL.class, vars);
/****************************************************************/
if (su != null) {
HttpHeaders h = new HttpHeaders();
h.setLocation(su.getUri());
return new ResponseEntity<>(su, h, HttpStatus.CREATED);
} else {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
And the POST method on service B called by service A using Spring RestTemplate looks like this:
@RequestMapping(value = "/linkreachable", method = RequestMethod.POST)
public ShortURL shortenerIfReachable(@RequestParam("url") String url,
@RequestParam(value = "sponsor", required = false) String sponsor,
@RequestParam(value = "brand", required = false) String brand,
HttpServletRequest request) {
ShortURL su = null;
boolean isReachableUrl = ping(url);
if (isReachableUrl){
su = createAndSaveIfValid(url, sponsor, brand, UUID
.randomUUID().toString(), extractIP(request));
System.out.println("URL REACHABLE!");
}
return su;
}
The RestTemplate method postForObject()
in Service A is giving me problems, always throwing HttpClientErrorException: 400 Bad Request
:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw
exception [Request processing failed; nested exception is org.springframework.web.client.H
ttpClientErrorException: 400 Bad Request] with root cause
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultR
esponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.ja
va:615)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:573)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:537)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:339
)
at urlshortener2014.mediumcandy.web.MediumCandyController.shortenerIfReachable(Med
iumCandyController.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.ja
va:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Invocabl
eHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(
InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMe
thod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdap
ter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdap
ter.handleInternal(RequestMappingHandlerAdapter.java:706)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(
AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:94
... etc
I followed some Spring tutorials to create my service and also checked the Spring Framework api docs but I couldn't get it to work.
source to share
Use Spring HATEOAS to solve this problem! When used ServiceA
and ServiceB
in, ServiceA
use Spring HATEOAS to get the controller / method runtime url in "ServiceB" like this:
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
...
String restURI = linkTo(methodOn(ServiceB.class).
shortenerIfReachable(null, null, null, null)).toString();
RestTemplate restTemplate = new RestTemplate();
su = restTemplate.postForObject(restURI, null, ShortURL.class, vars);
source to share
@Francisco you were almost there
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
...
String restURI = linkTo(methodOn(UrlShortenerControllerWithLogs.class).
shortenerIfReachable(url, null, null, null)).toString();
RestTemplate restTemplate = new RestTemplate();
su = restTemplate.postForObject(restURI, null, ShortURL.class);
You need to pass the url parameter when calling shortenerIfReachable and I also removed the vars from the postForObject call.
source to share