我如何将restTemplate中的参数传递给控制器​​的put方法

时间:2018-12-28 10:42:25

标签: java spring web resttemplate

我在传递参数时遇到问题。 我的收入应用程序中有名称,我想通过从其余应用程序传递新名称的参数来更新名称。 这是我的代码:

String url = "http://localhost:8084/rest/api/income/UpdateName/{oldName}/{newName}"; // the Url of the rest


    Map<String, String> params = new HashMap<>();
    params.put("oldName", oldName);
    params.put("newName", newName);
    Income income = new Income();

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.put(url, income, params);
    System.out.println(params);
}    

不幸的是,此代码无法正常工作,该怎么办?

这是控制器中的put方法:

@CrossOrigin
@GetMapping("/UpdateName/{oldName}/{clientName}") // view all incomes ..
public GeneralResponse viewAllIncome(@PathVariable("oldName") String oldName,@PathVariable("clientName") String clientName) {

    return new GeneralResponse(incomeServiceTemplate.updateClientName(oldName,clientName));

}    

1 个答案:

答案 0 :(得分:1)

端点是$group而不是db.collection.aggregate([ { "$group": { "_id": { "month": { "$month": "date" }, "year": { "$year": "date" }, "category": "$category" }, "credit": { "$sum": "$credit" } "debit": { "$sum": "$debit" } }}, { "$group": { "_id": { "month": { "$month": "$_id.month" }, "year": { "$year": "_id.year" }}, "categories": { "$push": { "category": "$category", "credit": "$credit", "debit": "$debit" } }, "credit": { "$sum": "$credit" } "debit": { "$sum": "$debit" } }}, { "$group": { "_id": "$_id.year", "months": { "$push": { "categories": "$categories", "credit": "$credit" "debit": "$debit" } } "credit": { "$sum": "$credit" } "debit": { "$sum": "$debit" } }} ]) ,只需在URL中附加参数并调用:

GET

或者有更好的方法,请使用PUT。它负责url编码:

String url = "http://localhost:8084/rest/api/income/UpdateName/"+oldName+"/"+newName;

RestTemplate restTemplate = new RestTemplate();
restTemplate.getForObject(url, Income.class);