org.springframework.web.HttpRequestMethodNotSupportedException。不支持请求方法 "GET"。

问题描述 投票:0回答:1

我正在学习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]

我的代码在这里:updateperson form。

 <!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
java forms spring-boot jpa put
1个回答
0
投票

请检查一下你的端点。

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

你的 GETPUT 方法有不同的路径,而你试图访问的是 GET 方法与 Put 路径如你在评论中所说。

你能做什么来修复它。

  • 改变 GET 通往 @GetMapping("updatePerson")
  • 打电话给你 GET 路径由这个URL。localhost:8080/update 不是 localhost:8080/updatePerson

0
投票

如果你检查URL:localhost:8080updatePerson?id=4&name=personame&age=34,你试图访问的方法(updatePerson)是一个。PUT所以你应该发一个 PUT 而非 GET.你可以在注释中看到方法类型。

@PutMapping("/updatePerson")

根据你的请求方式,应该有地方可以设置你使用的方法。另外,从URL来看,它似乎不适合任何请求,因为在updatePerson中,你期望在body中使用对象Person,但却发送: ?id=4&name=personame&age=34 作为查询参数。

一个带有body的curl例子,它将被你的方法接受。

curl -X PUT -H "Content-Type: application/json" -d '{"name":"personame","age":"34"}' http://localhost:8080/updatePerson 

0
投票

你发送了一个 GET 请求,而您已经添加了一个 @PutMapping 在你 updatePerson 方法。如果你想访问 updatePerson 那么你就应该尝试着用一个 PUT 方法。

使用 GET 方法时,访问 localhost:8080/update 并使用 PUT 方法时,访问 localhost:8080/updatePerson

© www.soinside.com 2019 - 2024. All rights reserved.