我正在尝试制作一个Spring Rest程序,但是当我尝试为我的一个元素调用GET方法时遇到以下错误
出现意外错误(type = Method Not Allowed,status = 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());
}
}
其他每种方法都有效。如何修复此错误?谢谢
答案 0 :(得分:1)
您可能正在尝试向此网址/company/flight/{id}
发送请求。您在类级别上有@RequestMapping
,当您指定类级别注释时,该URL将是相对的。因此,getById
方法的网址为/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);
}