java-Spring Framework和RestTemplate:无法使用REST服务

首先,我有两个Spring REST服务:服务A和服务B.服务A需要使用服务B公开的一些方法.A和B都是Spring @RestController.

服务A具有POST方法:

    @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);
        }
    }

服务A中使用Spring RestTemplate调用的服务B中的POST方法如下:

@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;
}

服务A中的RestTemplate方法postForObject()给我带来麻烦,总是抛出HttpClientErrorException:400错误的请求:

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

我已经按照一些Spring教程来构建我的服务,并且还检查了Spring Framework api-docs,但是我无法使其工作.

解决方法:

使用Spring HATEOAS来解决它!给定ServiceA和ServiceB,在ServiceA中使用Spring HATEOAS获取“ ServiceB”中控制器/方法的运行时URL,如下所示:

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);
上一篇:java-具有多个参数的RESTful Spring服务


下一篇:java-Spring RestTemplate-基于http状态代码读取不同的对象类型?