Java HTTP 405请求方法';把';不支持
我试图用spring boot做一个crud,我在postman中得到了这个更新错误 网址:http://localhost:8085/api/v1/student?18&name=student1&email=student@gmail.com 错误类型: “时间戳”:“2021-05-15T04:25:55.895+00:00”, “地位”:405, “错误”:“不允许使用方法”, “消息”:“不支持请求方法”PUT“, “路径”:“/api/v1/student” 这是我的studentService.java 这是我的存储库Java HTTP 405请求方法';把';不支持,java,spring,spring-boot,postman,put,Java,Spring,Spring Boot,Postman,Put,我试图用spring boot做一个crud,我在postman中得到了这个更新错误 网址:http://localhost:8085/api/v1/student?18&name=student1&email=student@gmail.com 错误类型: “时间戳”:“2021-05-15T04:25:55.895+00:00”, “地位”:405, “错误”:“不允许使用方法”, “消息”:“不支持请求方法”PUT“, “路径”:“/api/v1/student” 这是我的studentS
导入java.util.Optional;
导入org.springframework.data.jpa.repository.JpaRepository;
公共界面StudentRepository扩展了JpaRepository{
可选findStudentByEmail(字符串电子邮件);
}
在我看来,您使用的URL是错误的。它应该是http://localhost:8085/api/v1/student/18?name=student1
。
studenId
是路径变量,因此应该是路径的一部分。您在没有标识符的情况下将18添加到参数列表中。
此外,您可能还需要对URL进行编码或URL中的@
“消息”:“不支持请求方法‘PUT’,”路径“/api/v1/student”
告诉您它正在路径
/api/v1/student
中查找PUT定义,但没有。只有一个位于/api/v1/student/18
是的,你是对的,我现在使用api/v1/student/18,它可以工作,但是我如何对我的url进行编码路径变量的第二部分生成错误用%40替换@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.transaction.Transactional;
import java.lang.IllegalStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
private final StudentRepository studentRepository;
@Autowired
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public List<Student> GetStudents() {
return this.studentRepository.findAll();
}
public void addNewStudent(Student student) {
Optional<Student> studentOptional = studentRepository.findStudentByEmail(student.getEmail());
if (studentOptional.isPresent()) {
throw new IllegalStateException("email taken");
}
studentRepository.save(student);
}
public void deleteStudentByID(Long studentID) {
boolean exists = studentRepository.existsById(studentID);
if (!exists) {
throw new IllegalStateException("student with " + studentID + " does not exist!");
}
studentRepository.deleteById(studentID);
}
@Transactional
public void updateStudentByID(Long studentID, String name, String email)
{
Student student = studentRepository.findById(studentID)
.orElseThrow(() -> new IllegalStateException("student with id" + studentID + "does not exist"));
if (name != null && name.length() > 0 && !Objects.equals(student.getName(), name)) {
student.setName(name);
}
if (email != null && email.length() > 0 && !Objects.equals(student.getEmail(), email)) {
Optional<Student> studentOptional = studentRepository.findStudentByEmail(student.getEmail());
if (studentOptional.isPresent()) {
throw new IllegalStateException("email taken");
}
student.setEmail(email);
}
}
}
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> GetStudents() {
return studentService.GetStudents();
}
@PostMapping
public void registerNewStudent(@RequestBody Student student) {
studentService.addNewStudent(student);
}
@DeleteMapping(path = "{studentId}")
public void deleteStudent(@PathVariable("studentId") Long studentID) {
studentService.deleteStudentByID(studentID);
}
@PutMapping(path = "{studentId}")
public void updateStudent(@PathVariable("studentId") Long studentID, @RequestParam(required = false) String name,
@RequestParam(required = false) String email) {
studentService.updateStudentByID(studentID, name, email);
}
}
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StudentConfig {
@Bean
CommandLineRunner commandLineRunner(StudentRepository repository) {
return args -> {
Student st1= new Student("student11", "email", "17", 21);
Student st2= new Student("student22", "email", "17", 22);
repository.saveAll(List.of(st1, st2));
};
}
}
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<Student, Long> {
Optional <Student> findStudentByEmail(String email);
}