gpt4 book ai didi

java - 如何在Spring Controller 类中使用路径变量?

转载 作者:行者123 更新时间:2023-12-02 13:11:05 25 4
gpt4 key购买 nike

have seen我可以在类本身上方定义的 Requestmapping 注释中使用路径变量。但是,我无法让它发挥作用。我在这里缺少什么?

我的 Controller 类:

@Slf4j
@RestController
@RequestMapping(value = "{conncetion_name}/widget")
public class WidgetController {

private PortsRepository portsRepository;
private PortsService portsService;

@Autowired
public WidgetController(PortsRepository portsRepository, PortsService portsService) {
this.portsRepository = portsRepository;
this.portsService = portsService;
}

@RequestMapping(value = "/getBulkdata")
public List<TransferObject> getBulkData(@PathVariable(value="connection_name") String connectionName){

return portsService.convertPortsToTransferObject(portsRepository.findByroomTypeIsNotNull());

}
}

当我尝试访问给定的网址时,我收到一条错误消息:

Sat May 13 17:47:05 CEST 2017
There was an unexpected error (type=Internal Server Error, status=500).
Missing URI template variable 'connection_name' for method parameter of type String

我在这里缺少什么?

最佳答案

路径变量的请求映射注释:-

@RestController
@RequestMapping("/cities")
public class CityController {

private static final Logger logger = LoggerFactory.getLogger(CityController.class);

@Autowired
private CityService cityService;

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public RestResponse find(@PathVariable("id") Long id) {
.
.
.
}

或者你也可以这样使用:-

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public List<City> find(@PathVariable("id") Long id) {
.
.
.
}

关于java - 如何在Spring Controller 类中使用路径变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43955094/

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