Reputation: 6117
First of all, I have two Spring REST Services: Service A and Service B. Service A will need to consume some methods exposed by Service B. Both, A and B, are 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 in Service B called by Service A using Spring RestTemplate is the following:
@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 trouble, 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've followed some Spring Tutorials to build my Service, and also have checked the Spring Framework api-docs but I haven't been able to make it work.
Upvotes: 3
Views: 7100
Reputation: 1883
@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 have to pass the url parameter when calling shortenerIfReachable and I also removed the vars from the postForObject call.
Upvotes: 2
Reputation: 469
Use Spring HATEOAS to solve it! Given ServiceA
and ServiceB
, in ServiceA
use Spring HATEOAS for obtaining the runtime URL of the controller/method in 'ServiceB` as follows:
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);
Upvotes: 1
Reputation: 1883
Try the following url : curl --data "url=http://www.stackoverflow.com" http://localhost:8080/linkreachable
Upvotes: 0