gpt4 book ai didi

java - 错误 405 : Method not Allowed on DELETE and PUT

转载 作者:行者123 更新时间:2023-12-01 14:17:04 24 4
gpt4 key购买 nike

我关注了这个 spring tutorial 一切正常。在此之后,我决定添加 删除 修改 功能。我已经实现了它们,但是当我尝试使用它们时,我得到了以下信息 错误 :

{"status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/demo/delete"}

{"status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/demo/modify"}


我正在执行的命令:
curl localhost:8080/demo/delete -d name=First

curl localhost:8080/demo/modify -d name=First -d email=abc@gmail.com

//if the name doesn't exist in both methods, it will return "Nonexistent user!"
以下是以下代码:
MainController.java
package com.example.accessingdatamysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path="/demo")
public class MainController {
@Autowired
private UserRepository userRepository;

@PostMapping(path="/add")
public @ResponseBody String addNewUser (@RequestParam String name,
@RequestParam String email) {

User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved\n";
}

@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}

/* ----------------- NEW METHODS THAT I'VE CREATED ----------------- */

@DeleteMapping(path="/delete")
public String delete(@RequestParam String name) throws Exception {
if(userRepository.findByName(name).equals(null)) {
System.out.println("Nonexistent user!\n");
}
userRepository.deleteByName(name);
return "User successfully deleted!\n";
}

@PutMapping(path="/modify")
public String modify(@RequestParam String name, @RequestParam String email) throws Exception {
if(userRepository.findByName(name).equals(null)) {
System.out.println("Nonexistent user!\n");
}

userRepository.deleteByName(name);
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "User successfully modified!\n";
}
}
用户.java
package com.example.accessingdatamysql;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

private String name;

private String email;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
UserRepository.java
package com.example.accessingdatamysql;

import org.springframework.data.repository.CrudRepository;

import com.example.accessingdatamysql.User;

public interface UserRepository extends CrudRepository<User, Integer> {

public User findByName(String name);
public void deleteByName(String name);
}
我不知道这里发生了什么。我搜索了其他类似的问题,但错误相同,但没有一个解决了我的问题。

最佳答案

您正在使用方法 POST 执行调用而不是 DELETE/demo/deletePUT/demo/modify .

{"status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/demo/delete"}

{"status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/demo/modify"}

您没有展示如何执行那些失败的调用,但例如,如果您使用 GUI 客户端(例如 Postman)来模拟调用,请检查您是否选择了正确的 HTTP 方法。
如果您使用的是 curl终端中的库注意方法集:
curl -X "DELETE" http://your.url/demo/delete ...
curl -X "PUT" http://your.url/demo/modify ...

关于java - 错误 405 : Method not Allowed on DELETE and PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61781998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com