【问题标题】:Request method 'POST' not supported in Spring Boot any suggestions?Spring Boot 不支持请求方法“POST”有什么建议吗?
【发布时间】:2018-10-21 22:10:35
【问题描述】:

我正在使用 Spring Boot 和 Angular JS 构建一个 Web 应用程序,它将执行 CRUD 操作。我已经成功创建了一个类,在实现这个类时我收到了这个错误“不支持请求方法'POST'”

    My controller class is:

        package com.fyp.controller;

        import com.fyp.masterdata.Truck;
        import com.fyp.masterdata.TruckRepository;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.http.MediaType;
        import org.springframework.web.bind.annotation.*;

        import java.util.ArrayList;
        import java.util.List;

        @CrossOrigin(origins = "http://localhost:4200")
        @RestController
        public class TruckController {

            @Autowired
            TruckRepository truckRepository;


            @GetMapping(value="/truck",  produces= MediaType.APPLICATION_JSON_VALUE)
            public List<Truck> getAll1() {
                List<Truck> list = new ArrayList<>();
                Iterable<Truck> truck = truckRepository.findAll();
                truck.forEach(list::add);
                return list;
            }

            @PostMapping(value="/posttruck")
            public Truck postTruck(@RequestBody Truck truck) {
                truckRepository.save(new Truck(truck.getName(), truck.getCapacity()));
                return truck;
            }

            @GetMapping(value="/getTruckByName/{name}",  produces= MediaType.APPLICATION_JSON_VALUE)
            public List<Truck> getTruckByName(@PathVariable String name) {

                List<Truck> truck = truckRepository.getTruckByName(name);
                return truck;
            }

            @DeleteMapping(value="/truck/{id}")
            public void deleteTruck(@PathVariable long id){
                truckRepository.delete(id);
            }
        }

这是我在使用 POST 请求访问上述 URL 时遇到的错误。

    >#2018-05-11 18:42:46.868  WARN 10596 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound             : Request method 'POST' not supported

这是我用来访问 URL 的前端 Angular JS 代码。我正在运行 http://localhost:4200/truck 并试图让它工作。

    // Get all trucks
      getTruck(): Promise<Truck[]> {
        return this.http.get(this.truckUrl)
          .toPromise()
          .then(response => response.json() as Truck[])
          .catch(this.handleError);
      }
      getTruckByName(name: string): Promise<Truck[]> {
        const url = `/findbytruck/${name}`;
        return this.http.get(url)
          .toPromise()
          .then(response => response.json() as Truck)
          .catch(this.handleError);
      }

      create1(truck: Truck): Promise<Truck> {
        return this.http
          .post("truck", JSON.stringify(truck), {headers : this.headers})
          .toPromise()
          .then(res => res.json() as Truck)
          .catch(this.handleError);
      }

      delete1(id: number): Promise<void> {
        const url = `${this.truckUrl}/${id}`;
        return this.http.delete(url, {headers: this.headers})
          .toPromise()
          .then(() => null)
          .catch(this.handleError);
      }

Thank You.

【问题讨论】:

  • 你使用哪个网址?
  • 你能显示你的客户端代码吗?
  • 我已经编辑了代码并将我的前端 Angular JS 代码放在那里。希望对你有帮助
  • 好像打错了,应该是post("posttruck", JSON.stringify(truck), {headers : this.headers})
  • 您的 POST 网址是 'truck' 而不是 'posttruck'。将其更改为“posttruck”。

标签: java angular spring-boot


【解决方案1】:

也许你可以改变 @GetMapping(value="/truck", produces= MediaType.APPLICATION_JSON_VALUE) @PostMapping(value="/truck", produces= MediaType.APPLICATION_JSON_VALUE) 要么 @RequestMapping(value = "/truck", method = { RequestMethod.POST, RequestMethod.GET })

【讨论】:

  • 如果我没记错的话,GetMapping 没有问题,问题出在 PostMapping 中。但是将 POST 更改为 GET 也不能解决问题。
猜你喜欢
  • 2018-06-03
  • 2020-03-31
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多