简体   繁体   English

在 Spring RestTemplate 和 ResponseEntity 中设置状态码 301 或 303

[英]Set status code 301 or 303 in Spring RestTemplate and ResponseEntity

I've created a GET service to redirect to a webpage that uses POST.我创建了一个 GET 服务来重定向到使用 POST 的网页。 I'm trying to use restTemplate because with this I can send the body and header that request the service and I've achieved to get the information that a I need from the service.我正在尝试使用restTemplate因为这样我可以发送请求服务的正文和标题,并且我已经实现了从服务中获取我需要的信息。

However, I need to redirect to the server that has the POST service, but I couldn't, because I don't know how can a set the status code which I redirect to another server.但是,我需要重定向到具有 POST 服务的服务器,但我不能,因为我不知道如何设置重定向到另一台服务器的状态代码。

These are the functions that I'm using:这些是我正在使用的功能:

RequestEntity<Object> req = new RequestEntity<Object>(body, httpHeaders, HttpMethod.POST, url);

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, req, String.class);

You could do something like this as http post are not redirected automatically in spring.你可以做这样的事情,因为 http 帖子在 spring 中不会自动重定向。 So the following might help:因此,以下内容可能会有所帮助:

 public ResponseEntity<String> getData() {
 final RestTemplate restTemplate = new RestTemplate();
 String url = "http://localhost:8080/my-sample-post-url";
 final HttpComponentsClientHttpRequestFactory factory =
     new HttpComponentsClientHttpRequestFactory();
 final HttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); //org.apache.httpcomponents 
 factory.setHttpClient(httpClient);
 restTemplate.setRequestFactory(factory);
 Map<String, String> params = new HashMap<String, String>();
 ResponseEntity<String> response = restTemplate.postForEntity(url,params,String.class);
 if(response.getStatusCode().equals(HttpStatus.OK)) {
   return new ResponseEntity<String>(HttpStatus.SEE_OTHER);
 }
 else {
    return new ResponseEntity(HttpStatus.NOT_FOUND); // for example only
 }

 }

Note: Lax RedirectStrategy implementation that automatically redirects all HEAD, GET, POST, and DELETE requests.注意:自动重定向所有 HEAD、GET、POST 和 DELETE 请求的 Lax RedirectStrategy 实现。 This strategy relaxes restrictions on automatic redirection of POST methods imposed by the HTTP specification.此策略放宽了 HTTP 规范对 POST 方法的自动重定向的限制。 More here 更多在这里

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

相关问题 Spring RestTemplate处理自定义状态代码 - Spring RestTemplate handle custom status code 当控制器返回ResponseEntity时,如何在Filter中设置响应状态代码? - How to set response status code in Filter when controller returns ResponseEntity? Spring 启动:RestTemplate POST 调用不返回创建状态码 - Spring boot : RestTemplate POST call does not return created status code Spring RestTemplate - 根据http状态代码读取不同的对象类型? - Spring RestTemplate - Read different object type based on http status code? 如何设置 RestTemplate 以重试某些响应状态代码的调用? - How to set up RestTemplate to retry calls on certain response status code? 在 Web 客户端中,返回包含 Body 和 Http 状态码的 ResponseEntity(成功和失败) JAVA spring boot - In Web Client, returning ResponseEntity containing both Body and Http Status Code(both success and failure) JAVA spring boot 如何在Liferay MVC Portlet中的actionResponse中设置301状态代码 - How to set 301 status code in actionResponse in Liferay MVC portlet 如何将 ResponseEntity 设置为 Spring MVC 4 的 HTTP 404 - How to set ResponseEntity to HTTP 404 for Spring MVC 4 Spring 响应实体 - Spring ResponseEntity 在 restTemplate 之后设置弹簧过滤器 - set spring filter after restTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM