gpt4 book ai didi

java - Spring 休息: Request method 'GET' not supported

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

我正在尝试制作一个 Spring Rest 程序,但是当我尝试为我的一个元素调用 GET 方法时遇到以下错误

出现意外错误(类型=不允许的方法,状态=405)。不支持请求方法“GET”

这是我的 Controller 代码:

@RestController
@RequestMapping("/company/flight")
public class FlightController
{
private static final String template = "Hello, %s!";

@Autowired
private FlightRepo repo;

@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", defaultValue="World") String name) {
return String.format(template, name);
}

@RequestMapping( method= RequestMethod.GET)
public FlightList getAll(){
return repo.getAll();
}

@RequestMapping(value = "/flight/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getById(@PathVariable int id){

Flight fl = repo.findById(id);
if (fl==null)
return new ResponseEntity<String>("Flight not found",HttpStatus.NOT_FOUND);
else
return new ResponseEntity<Flight>(fl, HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.POST)
public Flight create(@RequestBody Flight fl){
repo.save(fl);
return fl;

}

@RequestMapping(value = "/flight/{id}", method = RequestMethod.PUT)
public Flight update(@RequestBody Flight fl) {
System.out.println("Updating flight ...");
repo.update(fl.getFlightId(),fl);
return fl;

}

@RequestMapping(value="/{id}", method= RequestMethod.DELETE)
public ResponseEntity<?> delete(@PathVariable int id){
System.out.println("Deleting flight ... " + id);
repo.deleteFlight(id);
return new ResponseEntity<User>(HttpStatus.OK);

}


@RequestMapping("/{flight}/id")
public String name(@PathVariable int id){
Flight result=repo.findById(id);
System.out.println("Result ..." + result);

return result.getAirport();
}

@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String flightError(Exception e) {
return e.getMessage();
}
}

我已经尝试了一些解决方案,但似乎没有任何效果。我看到其中大多数都在处理 POST 方法,但我只有一个 POST 方法,该方法与自定义值无关。

此方法调用时发生错误

@RequestMapping(value = "/flight/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getById(@PathVariable int id)
{

Flight fl = repo.findById(id);
if (fl==null)
return new ResponseEntity<String>("Flight not found",HttpStatus.NOT_FOUND);
else
return new ResponseEntity<Flight>(fl, HttpStatus.OK);
}

这就是我调用该方法的方式:

show(()-> System.out.println(flightclient.getById(20)));

这是我称之为的地方

public class Main {
private final static FlightClient flightclient=new FlightClient();
public static void main(String[] args) {
RestTemplate restTemplate=new RestTemplate();
flightclient.delete(20);
Flight fly=new Flight(20,"Tokyo","Paris",50,"2017-07-01");
try
{
show(()-> System.out.println(flightclient.create(fly)));
show(()->{
FlightList res=flightclient.getAll();
for(Flight u:res){
System.out.println(u.getFlightId()+" : "+u.getAirport());
}
});

show(()-> System.out.println(flightclient.getById(20)));
}catch(RestClientException ex){
System.out.println("Exception ... "+ex.getMessage());
}
}

所有其他方法都有效。我该如何修复此错误?谢谢

最佳答案

您可能正在尝试向此网址 /company/flight/{id} 发送请求。您在类级别上有一个 @RequestMapping ,当您指定类级别注释时,URL 将是相对的。因此,getById 方法的 URL 为 /company/flight/flight/{id}

getById的映射更改为@RequestMapping(value = "/{id}", method = RequestMethod.GET),然后就可以向发送请求>/company/flight/{id}.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<?> getById(@PathVariable int id)
{

Flight fl = repo.findById(id);
if (fl==null)
return new ResponseEntity<String>("Flight not found",HttpStatus.NOT_FOUND);
else
return new ResponseEntity<Flight>(fl, HttpStatus.OK);
}

关于java - Spring 休息: Request method 'GET' not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44096257/

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