如何从我的Spring Boot应用程序控制器创建对另一个API的PATCH请求?

时间:2019-02-06 14:01:38

标签: rest spring-boot

我想从我的Spring启动应用程序向另一个运行在localhost:9096的Springboot应用程序发送一个PATCH请求。我必须将某些参数编码到必须向其发送补丁请求的URL中。 这是我尝试过的事情:

final String url="http://localhost:9096/id/{id}/status/completed?reason=success&tag=1234";

    Map<String, String> parametersUri = new HashMap<String, String>();
    parametersUri.put("executions", executionPythonEntity.getExecutionId());

    restTemplate.exchange(url , HttpMethod.PATCH, new HttpEntity<>(parametersUri, headers), String.class, parametersUri);

1 个答案:

答案 0 :(得分:0)

您可以利用feignResttemplate

查看Resttemplate的此类问题

resttemplate示例:

HttpHeaders requestHeaders = new HttpHeaders();
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();

// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

// Make the HTTP POST request, marshaling the response to a String
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

查看此feign的问题

从文档中

@FeignClient("stores")
public interface StoreClient {
    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();

    @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
    Store update(@PathVariable("storeId") Long storeId, Store store);
}

如果您想要更详尽的答案,请详细说明您的问题。

我还要补充一点,还有其他几种发送请求的方式,但是我认为我提到的2种方式足以满足您的需求。