I am studying spring boot and spring jpa . I was able to successfully read and write into the database, but the problem occurs while updating the data.
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
I am trying to update data of table : Person ( Id, Name , Age) using a html form. It accepts the data, and using spring jpa I am trying to update it in MySQL database.
I am getting the following at console when trying to update the data, after submitting the form
> WARN 5176 --- [nio-8080-exec-1]
> .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
> [org.springframework.web.HttpRequestMethodNotSupportedException:
> Request method 'GET' not supported]
My code is here : updateperson form :
Title
Enter new details
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 {
List findByName(String name);
List findByAge(int age);
List 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
}
The URL to which I'm sending the request:
localhost:8080/updatePerson?id=4&name=personame&age=34