[英]Spring REST: Request method 'GET' not supported
我試圖制作一個Spring Rest程序,但是當我嘗試為我的一個元素調用GET方法時遇到以下錯誤
發生意外錯誤(類型=不允許使用方法,狀態= 405)。 請求方法“ GET”不受支持
這是我的控制器代碼:
@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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.