我正在学习 spring boot 和 spring jpa 。 我能够成功读写数据库,但更新数据时出现问题。
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
我正在尝试使用 html 表单更新表:Person(Id、Name、Age)的数据。它接受数据,并使用 spring jpa 我尝试在 MySQL 数据库中更新它。
提交表单后尝试更新数据时,我在控制台收到以下信息
> WARN 5176 --- [nio-8080-exec-1]
> .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
> [org.springframework.web.HttpRequestMethodNotSupportedException:
> Request method 'GET' not supported]
我的代码在这里: 更新者表格:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Enter new details</h1>
<form action="updatePerson" method="post">
Id : <input type="number" name="id"><br>
Name :<input type = "text" name = "name"><br>
Age : <input type = "number" name = "age"><br>
<input type="submit">
</form>
</body>
</html>
PersonRepository.java
package com.example.demo.repository;
import com.example.demo.model.Person;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface PersonRepository extends CrudRepository<Person , Integer> {
List<Person> findByName(String name);
List<Person> findByAge(int age);
List<Person> findAll();
}
PersonController.java
package com.example.demo.controller;
import com.example.demo.model.Person;
import com.example.demo.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class PersonController {
@Autowired
PersonRepository personRepository;
@GetMapping("update")
public String updatePerson(){
System.out.println("update to updateperson html");
return "updateperson";
}
@PutMapping("/updatePerson")
public String updatePerson(Person person){
System.out.println("UPDATING TO" + person.getAge()+" : "+person.getName() + " : "+person.getPid());
personRepository.save(person);
return "updateperson";
}
}
Person.java
package com.example.demo.model;
import org.springframework.stereotype.Component;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
int pid;
String name;
int age;
public Person() {
}
public Person(int pid, String name, int age) {
this.pid = pid;
this.name = name;
this.age = age;
}
//getter and setters
}
我将请求发送到的 URL:
localhost:8080/updatePerson?id=4&name=personame&age=34
最佳答案
更好地了解您的端点:
@GetMapping("update")
public String updatePerson(){
System.out.println("update to updateperson html");
return "updateperson";
}
@PutMapping("/updatePerson")
public String updatePerson(Person person){
System.out.println("UPDATING TO" + person.getAge()+" : "+person.getName() + " : "+person.getPid());
personRepository.save(person);
return "updateperson";
}
您的 GET
和 PUT
方法具有不同的路径,并且您尝试使用 Put
路径访问 GET
方法你在评论中说。
你可以采取什么措施来解决这个问题:
- 将
GET
路径更改为@GetMapping("updatePerson")
- 通过以下 URL 调用您的
GET
路径:localhost:8080/update
而不是localhost:8080/updatePerson
关于java - org.springframework.web.HttpRequestMethodNotSupportedException : Request method 'GET' not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62156867/