简体   繁体   English

GET 请求适用于 Postman,但不适用于 SpringBoot RestTemplate

[英]GET Request Works in Postman but not with SpringBoot RestTemplate

I have a two Spring Boot application.我有两个 Spring Boot 应用程序。 One is a rest client that makes rest calls.一个是进行休息调用的休息客户端。 Another app that has only Rest endpoint.另一个只有 Rest 端点的应用程序。

When the Rest client hits the rest endpoint, it fails.当 Rest 客户端访问 rest 端点时,它会失败。

This is the code used to hit the rest endpoint:这是用于命中其余端点的代码:

HttpHeaders headers = new HttpHeaders();
headers.set(ACCEPT, APPLICATION_JSON);
headers.set(CONTENT_TYPE, APPLICATION_JSON);
HttpEntity entity = new HttpEntity(headers);

UriComponentsBuilder builder = UriComponentsBuilder
    .fromHttpUrl(url)

    .queryParam(EMAIL, URLEncoder.encode(email, "UTF-8"))
    .queryParam(ADDRESS, URLEncoder.encode(address, "UTF-8"));

ResponseEntity<Address> response = 
commonRestTemplate
    .exchange(builder.toUriString(), 
HttpMethod.GET, entity, Address.class);

This is the rest endpoint the client is trying to hit:这是客户端尝试访问的其余端点:

@RestController
@AllArgsConstructor
public class AddressController {
    private final RestTemplate commonRestTemplate;

    // constructor and other rest endpoints


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<Address> getAddress(@RequestParam String email, @RequestParam String address) {
        try {
            // do soemthing
        } catch (RuntimeException e) 
        {
            LOGGER.error(e.getMessage(), e);
            return status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
   }
}

This is the error I'm seeing in the app with the rest endpoint:这是我在带有其余端点的应用程序中看到的错误:

2020-03-26 16:33:53.619  WARN 9 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'address' is not present]

2020-03-26 16:50:02.691 ERROR 9 --- [nio-8080-exec-9] u.c.h.s.s.controller.AddressController      : Key may not be empty

Why does the Rest call work with Postman but not my rest client?为什么 Rest 调用适用于 Postman 而不是我的 Rest 客户端?

I've also tried with and without encoding the special characters in the rest client with no luck.我也尝试过在其他客户端中对特殊字符进行编码和不编码,但都没有运气。 I can't seem to see what I am missing我似乎看不到我错过了什么

Try below changes尝试以下更改

UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(url)
.queryParam("email", URLEncoder.encode(email, "UTF-8"))
.queryParam("address", URLEncoder.encode(address, "UTF-8"));

@RestController
@AllArgsConstructor
public class AddressController {
private final RestTemplate commonRestTemplate;

// constructor and other rest endpoints


@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<Address> getAddress(@RequestParam("email") String email, @RequestParam("address") String address) {
    try {
        // do soemthing
    } catch (RuntimeException e) 
    {
        LOGGER.error(e.getMessage(), e);
        return status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
 }
}

can be 2 issues:可以是2个问题:

  1. static ADDRESS is properly defined and referring to "address".静态地址被正确定义并指代“地址”。
  2. another one, address value is not null.另一个,地址值不为空。 print address value before calling restTemplate.在调用 restTemplate 之前打印地址值。

I had this problem too.我也有这个问题。 It was solved when I used uri instead string in exchange method.当我在交换方法中使用 uri 而不是 string 时,它解决了。

ResponseEntity<String> responseEntity = null;
Map<String, String> map = generate map to keep key and value of necessaryparameters;
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl("SERVICE_URL");
map.forEach((k, v) -> {
    uriComponentsBuilder.queryParam(k, v);
});
URI uri = uriComponentsBuilder.build(false).encode("windows-1256").toUri();
responseEntity = new RestTemplate().exchange(uri, HttpMethod.POST, request, String.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestTemplate 交换超时但 PostMan 有效 - RestTemplate Exchange Timeouts but PostMan Works Spring Boot restTemplate POST请求bad_certificate,但在SOAPUI和Postman中有效 - Spring Boot restTemplate POST request bad_certificate, but works in SOAPUI and Postman RestTemplate GET Request 中的 handshake_failure 在浏览器中工作 - handshake_failure in RestTemplate GET Request which works in browser JSON 格式的 RestTemplate 中的 GET 请求 - GET request in RestTemplate in JSON Get 请求适用于 POSTMAN 但在我的 Java 应用程序中引发垃圾响应 - Get request works with POSTMAN but throws garbage response in my Java application REST POST 与 POSTMAN 一起正常工作,但在使用 Spring RestTemplate 时出现异常 - REST POST works correctly with POSTMAN but exception when using Spring RestTemplate Java - 尽管邮递员调用有效,但不允许使用 RestTemplate 405 方法 - Java - RestTemplate 405 Method Not Allowed Although Postman Call Works Java Spring RestTemplate POST问题,而Postman工作 - Java Spring RestTemplate POST issue, while Postman works Spring restTemplate 不适用于 URL 中的单引号。 与邮递员一起工作 - Spring restTemplate not working for single quotes in URL. Works with postman 用于JSON有效负载的GET请求的RestTemplate - RestTemplate for GET request with JSON payload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM