[英]Request method 'POST' not supported in Spring Boot any suggestions?
I'm building a web application using Spring Boot and Angular JS which will perform CRUD operations. 我正在使用Spring Boot和Angular JS构建将执行CRUD操作的Web应用程序。 I have successfully created one class and when implementing this class I'm getting this error "Request method 'POST' not supported"
我已经成功创建了一个类,并且在实现该类时遇到此错误“请求方法'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);
}
}
This is the error which I'm getting when I hit the above URL with POST request. 这是我通过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
This is my front-end Angular JS code which is used to hit the URL. 这是我的前端Angular JS代码,用于访问URL。 I'm running http://localhost:4200/truck and trying to get it work.
我正在运行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.
也许您可以将@GetMapping(value="/truck", produces= MediaType.APPLICATION_JSON_VALUE)
更改为@PostMapping(value="/truck", produces= MediaType.APPLICATION_JSON_VALUE)
或@RequestMapping(value = "/truck", method = { RequestMethod.POST, RequestMethod.GET })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.