gpt4 book ai didi

java - 如何解决spring controller中请求映射 "ambiguous"的情况?

转载 作者:搜寻专家 更新时间:2023-11-01 02:01:44 24 4
gpt4 key购买 nike

我编写了一个 spring Controller ,其中我只想对所有方法使用单个 URL。即使我使用不同的方法签名 int, string, object 我也会收到错误。

@RequestMapping(value="problemAPI/ticket", method = RequestMethod.GET )
public @ResponseBody String getTicketData(@RequestParam("customerId") int customerId) {
return "customer Id: "+customerId+" has active Ticket:1010101";
}

@RequestMapping(value="problemAPI/ticket", method = RequestMethod.GET )
public @ResponseBody String getTicketStatusByCustname(@RequestParam("customerName") String customerName) {
return "Mr." + customerName + " Your Ticket is Work in Progress";
}

@RequestMapping(value="problemAPI/ticket", method = RequestMethod.POST )
public @ResponseBody String saveTicket(@RequestBody TicketBean bean) {
return "Mr." + bean.getCustomerName() + " Ticket" + bean.getTicketNo() + " has been submitted successfuly";
}

错误:

java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'problemTicketController' bean method 
public String com.nm.controller.webservice.ticket.problem.ProblemTicketController.getTicketData(int)
to {[/problemAPI/ticket],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'problemTicketController' bean method
public java.lang.String com.nm.controller.webservice.ticket.problem.ProblemTicketController.getTicketByCustname(int) mapped.

最佳答案

您可以通过使用 params 注释属性显式指定查询参数来实现此目的:

@RequestMapping(
value = "problemAPI/ticket",
params = "customerId",
method = RequestMethod.GET
)
public @ResponseBody String getTicketData(@RequestParam("customerId") int customerId){
return "customer Id: " + customerId + " has active Ticket:1010101";
}

@RequestMapping(
value = "problemAPI/ticket",
params = "customerName",
method = RequestMethod.GET
)
public @ResponseBody String getTicketStatusByCustname(@RequestParam("customerName") String customerName){
return "Mr." + customerName + " Your Ticket is Work in Progress";
}

为了使其更清晰,您可以使用别名注释,例如 @GetMapping@PostMapping:

@GetMapping(
value = "problemAPI/ticket",
params = "customerName"
)
public @ResponseBody String getTicketStatusByCustname(@RequestParam("customerName") String customerName) {

}

@PostMapping(
value = "problemAPI/ticket"
)
public @ResponseBody String saveTicket(@RequestBody TicketBean bean) {
return "Mr." + bean.getCustomerName() + " Ticket" + bean.getTicketNo() + " has been submitted successfuly";
}

关于java - 如何解决spring controller中请求映射 "ambiguous"的情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44429656/

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