我在应用程序中创建了两个表,一个用于用户,另一个用于宠物。我实现了两个控制器,它们将获取所有,插入,更新和删除连接到mongo db的阵列列表。 getAll和delete函数正常工作,但是当我尝试使用POST或PUT更新或插入时,出现405错误-请求方法'GET'不支持
宠物管理员是:
@RestController
@RequestMapping(value = "/index/pets")
@Api(name = "Pet Adoptions ", description = "Provides a list of methods that manage Pet Adoptions", stage = ApiStage.RC)
public class PetController {
private PetRepository petRepository;
public PetController(PetRepository petRepository)
{
this.petRepository = petRepository;
}
//@RequestMapping (value = "/all", method = RequestMethod.GET)
@ApiMethod(description = "Get all pets from Database")
@GetMapping("/all")
public List<PetAdoption> getAll() {
return petRepository.findAll();
}
@ApiMethod(description = "create new pet")
@PutMapping
public void insert (@RequestBody PetAdoption petAdoption)
{
this.petRepository.insert(petAdoption);
//this.petRepository.findAll();
}
@ApiMethod(description = "this will insert/update depending on whether pet exists")
@PostMapping
public void updatePet(@RequestBody PetAdoption petAdoption){
this.petRepository.save(petAdoption);
//this.petRepository.findAll();
}
@ApiMethod (description = "this will remove a pet")
@RequestMapping(value="/delete/{id}", method = {RequestMethod.DELETE, RequestMethod.GET})
public void delete (@PathVariable("id") String id){
this.petRepository.deleteById(id);
this.petRepository.findAll();
}
我尝试为每种方法添加将@ Put / Post映射更改为:
@RequestMapping( method = {RequestMethod.POST, RequestMethod.GET})
@RequestMapping( method = {RequestMethod.PUT, RequestMethod.GET})
并尝试将@RequestMapping更改为:
@RequestMapping(value = "/index/pets", {method = {RequestMethod.PUT, RequestMethod.POST, RequestMethod.GET})
我遇到一个错误。这是我尝试在邮递员中对本地主机:8082 / index / pets执行PUT或POST请求时得到的响应:
{
"timestamp": "2019-09-10T19:16:15.861+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/index/pets"
}
完整的答复在这里
答案 0 :(得分:0)
基于输出,我认为不要求PUT或POST。错误显示“不支持”。