1

我正在编写 JUnit 测试,通过 RestTemplate 调用我的应用程序。我已经成功实现了 GET、POST 和 PUT,但无法运行 PATCH(尽管它在客户端发送 URL 时有效)。例如,POST 使用以下代码运行:

    RestTemplate restTemplate = new RestTemplate(); 
    ProductModel postModel = restTemplate.postForObject(TestBase.URL + URL, pModel, ProductModel.class);            

但是当我尝试调用我在网上找到的 restTemplate.patchForObject() 时,STS 返回一个错误,指出该函数未定义。我因此使用了这个:

    RestTemplate restTemplate = new RestTemplate(); 
    ResponseEntity<MessageModel> retval = restTemplate.exchange("http://localhost:8080/products/batchUpdateProductPositions", 
            HttpMethod.PATCH, new HttpEntity<ProductPositionListModel>(pps), MessageModel.class);   

编译,但给我一个错误:

I/O Error on PATCH request for "http://localhost:8080/products/batchUpdateProductPositions": Invalid HTTP method: PATCH

在应用程序中,我在 Controller 类中定义了操作:

@RequestMapping(value = "/batchUpdateProductPositions", method = RequestMethod.PATCH)
public MessageModel batchUpdatePosition(
        @RequestBody ProductPositionListModel productPositionList)
        throws Exception {
    try {
        return productService.batchUpdatePosition(productPositionList);
    } catch (Exception e) {

我在'try'块内的return语句上放了一个断点,但是当我在调试下运行它时它从未跳闸。

谁能告诉我我在哪里绊倒了?

4

5 回答 5

5

默认情况下RestTemplate使用标准 JDK HttpURLConnectionHTTP 客户端发出请求。此客户端不支持PATCH方法。您可以RestTemplate通过客户端工厂配置使用其他 HTTP 客户端,例如HttpComponentsClientHttpRequestFactoryOkHttpClientHttpRequestFactory.

HttpClient client = HttpClients.createDefault();
RestTemplate template= new RestTemplate();
template.setRequestFactory(new HttpComponentsClientHttpRequestFactory(client)); 

您还需要添加适当的依赖项,例如org.apache.httpcomponents:httpclient:$versionHTTP 组件客户端。

于 2017-01-10T09:09:08.280 回答
2

我发现了一个与 JUnit 代码的其余部分保持一致的解决方案。使用 postForObject(),您可以在这种情况下传入您需要的 HTTP 方法:

    MessageModel pModel = restTemplate.postForObject(TestBase.URL + URL + "/batchUpdateProductPositions?_method=patch", 
            pps, MessageModel.class);           

这运行正确。不能说是否有我没有注意到的副作用。

于 2017-01-10T14:35:31.393 回答
1

这对我有用,用于使用 RestTemplate 执行 PATCH 请求:

RestTemplate restTemplate = new RestTemplate();

HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectTimeout(timeout);
httpRequestFactory.setReadTimeout(timeout);

restTemplate.setRequestFactory(httpRequestFactory);

现在将此restTemplate 与exchange() 一起使用。

所需的依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
</dependency> 
于 2018-03-15T12:43:21.893 回答
1

试试这个:

import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
//...
RestTemplate rest = new RestTemplate(new HttpComponentsClientHttpRequestFactory())

//Now make the PATCH call using Exchange
ResponseEntity<Map<String, Object>> response = rest.exchange(api, HttpMethod.PATCH, request, responseType);

不要忘记将其添加到您的依赖项中:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.4.1</version>
 </dependency>
于 2019-05-15T10:30:57.490 回答
0

此解决方案

MessageModel pModel = restTemplate.postForObject(TestBase.URL + URL + "/batchUpdateProductPositions?_method=patch",pps, MessageModel.class);   

如果您使用的是 Post Method( @PostMapping),这很有用,如果您想使用 Patch Method( @PatchMapping),试试这个:

restTemplate.patchForObject("http://localhost:8080/products/batchUpdateProductPositions", requestEntity, String.class);
于 2018-02-15T16:16:10.130 回答