简体   繁体   English

将 HttpServletRequest 传递给另一个 API controller

[英]Passing HttpServletRequest to another API controller

I have a controller that receives HttpServletRequest with body form-data with some key-value我有一个 controller 接收带有一些key-value正文form-dataHttpServletRequest

I want to pass this HttpServletRequest to another API.我想将此HttpServletRequest传递给另一个 API。 That another API controller receives HttpServletRequest另一个 API controller 收到HttpServletRequest

So far, I've tried this in my service class.到目前为止,我已经在我的服务 class 中尝试过这个。

public void saveApi(HttpServletRequest request) throws Exception {
    System.out.println(request.getCode());
    RestTemplate restTemplate = new RestTemplate();         
    HttpServletRequest result = restTemplate.postForObject( "http:localhost:8080/save", request, HttpServletRequest .class);
}

The result is the other API java.lang.NullPointerException: null , other API didn't receive my post.结果是另一个 API java.lang.NullPointerException: null ,其他 ZDB97442387143ACE143 没有收到我的帖子。

How to do that?怎么做? Is it possible?可能吗?

Another service, for example: GET method, I use the RestTemplate to call to another API with some parameter, it succeeds.另一个服务,例如: GET方法,我使用RestTemplate调用另一个 API 并带有一些参数,它成功。

UPDATE更新

the answer is:答案是:

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("name", request.getParameter("name"));
form.add("blabla", request.getParameter("blabla"));
form.add("blabla", request.getParameter("blabla"));

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(form, httpHeaders);
restTemplate.postForEntity("http:localhost:8080/save", requestEntity, String.class);

Using RestTemplate::postForObject and this class in general you have to pass the correct request object:使用RestTemplate::postForObject和这个 class 通常你必须传递正确的request object:

The request parameter can be a HttpEntity in order to add additional HTTP headers to the request, that is HttpEntity with body and headers.请求参数可以是 HttpEntity,以便向请求添加额外的 HTTP 标头,即带有正文和标头的HttpEntity

Note the HttpServletRequest extends ServletRequest .注意HttpServletRequest扩展了ServletRequest

  1. Extract body using either ServletRequest::getInputStream or ServletRequest::getReader .使用ServletRequest::getInputStreamServletRequest::getReader提取正文。 In case of multipart/form-data use HttpServletRequest::getParts如果是multipart/form-data ,请使用HttpServletRequest::getParts
  2. Serialize the bytes of body to an object that is required for POST http:localhost:8080/save .将 body 的字节序列化为POST http:localhost:8080/save所需的 object。
  3. Use the object and create new HttpEntity(T body) .使用 object 并创建new HttpEntity(T body)
  4. Pass it as request inside the RestTemplate::postForObject method.RestTemplate::postForObject方法中将其作为request传递。

The exact steps might differ regarding what data you need, the point is you cannot pass HttpServletRequest directly but use an instance of HttpEntity .关于您需要的数据,确切的步骤可能会有所不同,关键是您不能直接传递HttpServletRequest而是使用HttpEntity的实例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM