【问题标题】:Spring REST: Request method 'GET' not supportedSpring REST:不支持请求方法“GET”
【发布时间】:2017-10-21 03:08:39
【问题描述】:

我正在尝试制作 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());
    }
}

所有其他方法都有效。如何解决此错误?谢谢

【问题讨论】:

  • 你的要求是什么?
  • 你的客户是什么?
  • 我使用 Intellij,并使用 Gradle 添加了 Spring 支持
  • show(()-> System.out.println(flightclient.getById(20)));具体叫什么名字?

标签: java spring rest spring-mvc


【解决方案1】:

您可能正尝试向此 URL /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);
}

【讨论】:

  • 成功了。奇怪的是它以前没有用过,而 /flight/{id} 是我尝试过的解决方案之一。谢谢:D
猜你喜欢
  • 1970-01-01
  • 2018-12-24
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 2023-04-09
  • 2015-10-02
相关资源
最近更新 更多