Spring restcontroller Spring Rest模板-删除操作失败,请求错误

Spring restcontroller Spring Rest模板-删除操作失败,请求错误,spring-restcontroller,spring-rest,Spring Restcontroller,Spring Rest,我正在使用SpringREST模板执行删除操作 我收到了400个错误的请求。然而,邮递员也提出了同样的要求。 网址: 控制器代码: @DeleteMapping(value = "/customer/{customer-number}/customer-items/{country}", params = {"uline-item-number"} , produces = {"application/json"}) public ResponseEntity<Boolean&

我正在使用SpringREST模板执行删除操作

我收到了400个错误的请求。然而,邮递员也提出了同样的要求。 网址:

控制器代码:

     @DeleteMapping(value = "/customer/{customer-number}/customer-items/{country}", params = {"uline-item-number"} , produces = {"application/json"})

public ResponseEntity<Boolean> deleteCustomerItem( @PathVariable("customer-number") final String customerNumber, 
               @PathVariable("country") final String countryCode,
                @RequestParam("productCode") final String productCode) {
            try {
                return new ResponseEntity<>(appCustomerService.deleteCustomerItem(customerNumber, countryCode, productCode), HttpStatus.OK);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }
        }
public Boolean deleteCustomerItem(String customerNumber, String countryCode, String productCode)
            throws Exception{
        Map<String, String> uriVariables = new HashMap<>();
        uriVariables.put("productCode", productCode);
        String productUrl = http://localhost:8080/product-service/customer/123456/customer-items/US";
        try {
            restTemplate.exchange(productUrl , HttpMethod.DELETE, HttpEntity.EMPTY, Void.class, uriVariables);
            return true;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
@DeleteMapping(value=“/customer/{customer number}/customer items/{country}”,params={“uline item number”},products={“application/json”})
public ResponseEntity deleteCustomerItem(@PathVariable(“客户编号”)最终字符串customerNumber,
@PathVariable(“country”)最终字符串countryCode,
@RequestParam(“productCode”)最终字符串(productCode){
试一试{
返回新的ResponseEntity(appCustomerService.deleteCustomerItem(customerNumber、countryCode、productCode),HttpStatus.OK);
}捕获(例外e){
logger.error(e.getMessage(),e);
返回新的响应属性(HttpStatus.BAD_请求);
}
}
服务实施:

     @DeleteMapping(value = "/customer/{customer-number}/customer-items/{country}", params = {"uline-item-number"} , produces = {"application/json"})

public ResponseEntity<Boolean> deleteCustomerItem( @PathVariable("customer-number") final String customerNumber, 
               @PathVariable("country") final String countryCode,
                @RequestParam("productCode") final String productCode) {
            try {
                return new ResponseEntity<>(appCustomerService.deleteCustomerItem(customerNumber, countryCode, productCode), HttpStatus.OK);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }
        }
public Boolean deleteCustomerItem(String customerNumber, String countryCode, String productCode)
            throws Exception{
        Map<String, String> uriVariables = new HashMap<>();
        uriVariables.put("productCode", productCode);
        String productUrl = http://localhost:8080/product-service/customer/123456/customer-items/US";
        try {
            restTemplate.exchange(productUrl , HttpMethod.DELETE, HttpEntity.EMPTY, Void.class, uriVariables);
            return true;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }
public Boolean deleteCustomerItem(字符串customerNumber、字符串countryCode、字符串productCode)
抛出异常{
Map uriVariables=newhashmap();
uriVariables.put(“productCode”,productCode);
字符串productUrl=http://localhost:8080/product-服务/客户/123456/客户项目/US”;
试一试{
exchange(productUrl、HttpMethod.DELETE、HttpEntity.EMPTY、Void.class、uriVariables);
返回true;
}捕获(例外e){
抛出新异常(如getMessage());
}
}

请求中是否缺少任何内容?请帮助我解决此问题。

您弄乱了路径参数和查询参数。以下操作应正常工作:

    String url = "http://localhost:8080/product-service/customer/{customer-number}/customer-items/{country}";

    // Path parameters should be here
    Map<String, String> uriParams = new HashMap<>();
    uriParams.put("customer-number", "123456");
    uriParams.put("country", "US");

    URI productUri = UriComponentsBuilder.fromUriString(url)            
            .queryParam("productCode", productCode) // query parameters should be here
            .buildAndExpand(uriParams)
            .toUri();

    restTemplate.exchange(productUri, HttpMethod.DELETE, HttpEntity.EMPTY, Void.class);
stringurl=”http://localhost:8080/product-服务/customer/{customer number}/customer items/{country}”;
//路径参数应该在这里
Map uriParams=newhashmap();
uriParams.put(“客户编号”、“123456”);
uriParams.put(“国家”、“美国”);
URI productUri=UriComponentsBuilder.FromUrString(url)
.queryParam(“productCode”,productCode)//查询参数应该在这里
.buildAndExpand(uriParams)
.toUri();
交换(productUri,HttpMethod.DELETE,HttpEntity.EMPTY,Void.class);

您弄乱了路径参数和查询参数。以下操作应正常工作:

    String url = "http://localhost:8080/product-service/customer/{customer-number}/customer-items/{country}";

    // Path parameters should be here
    Map<String, String> uriParams = new HashMap<>();
    uriParams.put("customer-number", "123456");
    uriParams.put("country", "US");

    URI productUri = UriComponentsBuilder.fromUriString(url)            
            .queryParam("productCode", productCode) // query parameters should be here
            .buildAndExpand(uriParams)
            .toUri();

    restTemplate.exchange(productUri, HttpMethod.DELETE, HttpEntity.EMPTY, Void.class);
stringurl=”http://localhost:8080/product-服务/customer/{customer number}/customer items/{country}”;
//路径参数应该在这里
Map uriParams=newhashmap();
uriParams.put(“客户编号”、“123456”);
uriParams.put(“国家”、“美国”);
URI productUri=UriComponentsBuilder.FromUrString(url)
.queryParam(“productCode”,productCode)//查询参数应该在这里
.buildAndExpand(uriParams)
.toUri();
交换(productUri,HttpMethod.DELETE,HttpEntity.EMPTY,Void.class);

你是对的。我现在意识到了问题。更新了代码,它正在工作。谢谢你的帮助。你是对的。我现在意识到了问题。更新了代码,它正在工作。谢谢你的帮助。