错误 405:DELETE 和 PUT 不允许使用方法

Error 405: Method not Allowed on DELETE and PUT

我遵循了这个 spring tutorial 并且一切正常。在此之后,我决定添加 deletemodify 功能。我已经实现了它们,但是当我尝试使用它们时,我得到了以下 error:

{"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";
    }
}

User.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);
}

我不知道这里发生了什么。我已经搜索了其他类似的问题,但有相同的错误,但 none 解决了我的问题。

这就是 @DeleteMapping@PutMapping 的工作方式。这些注释是 @RequestMapping 的快捷方式,method 属性相应地固定为 DELETEPUT。要允许端点使用多个 HTTP 方法,您应该明确列出它们:

@RequestMapping(path = "/modify", method = { RequestMethod.DELETE, RequestMethod.POST })
public String modify(...){
   ...
}

您正在使用 POST 方法执行调用,而不是 /demo/deleteDELETE/demo/modifyPUT

{"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"}

您没有展示如何执行那些失败的调用,但是例如,如果您使用 Postman 等 GUI 客户端来模拟调用,请检查您是否选择了正确的 HTTP 方法。 如果您在终端中使用 curl 库,请注意方法集:

curl -X "DELETE" http://your.url/demo/delete ...
curl -X "PUT" http://your.url/demo/modify ...

按照@Dez 的回答后,问题解决了,但出现了其他错误:

No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call

我通过在 DELETE 上添加 @TransactionalMainController[=27 中的 PUT 方法解决了这个问题=] class:

@DeleteMapping(path="/delete")
@Transactional //added
public void delete(@RequestParam String name) throws Exception { (...) }

@PutMapping(path="/modify")
@Transactional //added
public void modify(@RequestParam String name, @RequestParam String email) throws Exception { (...) }

并修改了异常抛出条件,即总是返回false:

if(userRepository.findByName(name).equals(null)) {
     throw new Exception("Nonexistent user!");
}

if(userRepository.findByName(name) == null) {
     throw new Exception("Nonexistent user!");
}