gpt4 book ai didi

rest - restTemplate.exchange()方法有什么作用?

转载 作者:行者123 更新时间:2023-12-03 10:28:57 25 4
gpt4 key购买 nike

其实restTemplate.exchange()方法有什么作用?

@RequestMapping(value = "/getphoto", method = RequestMethod.GET)
public void getPhoto(@RequestParam("id") Long id, HttpServletResponse response) {

logger.debug("Retrieve photo with id: " + id);

// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.IMAGE_JPEG);

// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<String> entity = new HttpEntity<String>(headers);

// Send the request as GET
ResponseEntity<byte[]> result =
restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}",
HttpMethod.GET, entity, byte[].class, id);

// Display the image
Writer.write(response, result.getBody());
}

最佳答案

method documentation非常简单:

Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity.

URI Template variables are expanded using the given URI variables, if any.



考虑从您自己的问题中提取的以下代码:

ResponseEntity<byte[]> result = 
restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}",
HttpMethod.GET, entity, byte[].class, id);

我们有以下内容:
  • 将对给定URL执行 GET 请求,以发送包装在 HttpEntity 实例中的HTTP header 。
  • 给定的URL包含模板变量({id})。它会被最后一个方法参数(id)中给出的值替换。
  • 响应实体将以包裹在 byte[] 实例中的ResponseEntity的形式返回。
  • 关于rest - restTemplate.exchange()方法有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20186497/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com