有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

即使在springboot和H2中出现404错误之后,java数据也会被保存和删除

我是H2和Spring引导组合的新手,我正在使用所有最新更新创建一个简单的rest API,我在控制器类中创建了多个端点来操作和显示数据,但无论何时发布新对象,我都会在postman中看到404错误,但我仍然能够看到插入h2 DB的数据,这与删除操作相同,我得到404,但它仍然会从表中删除数据,我很确定我犯了一个非常愚蠢的错误,我试着调试它,但没有发现任何实质性的错误。下面是我的课程-

控制器-

package com.nerdbot.student.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nerdbot.student.entity.Student;
import com.nerdbot.student.entity.repo.StudentRepo;

@Controller
@RequestMapping("/")
public class StudentController {


    @Autowired
    StudentRepo repo;
      @RequestMapping(value="student",method=RequestMethod.GET)
        public @ResponseBody Student getValueByid(@RequestParam(value="id", required=true) String id) {
          Student s = repo.findOne(Long.valueOf(id).longValue());
          return s;
        }

      @RequestMapping(value="allstudent",method=RequestMethod.GET)
        public @ResponseBody List<Student> getAllValue() {
          List<Student>s = repo.findAll();
          return s;
        }

      @RequestMapping(value="delete",method=RequestMethod.GET)
        public @ResponseBody String getdeleteById(@RequestParam(value="id", required=true) String id) {
          repo.delete(Long.valueOf(id).longValue());
          return id + "- deleted ";
        }

      @RequestMapping(value="add", method = RequestMethod.POST)
        public String add( @RequestBody Student input) {

          try {
            repo.save(input);

        } catch (Exception e) {
            System.err.println(e.getStackTrace());
            e.printStackTrace();
        }

          return "Saved";

        }


      @RequestMapping(value="deleteAll",method=RequestMethod.GET)
        public String getdeleteById() {
          repo.deleteAll();;
          return "All record deleted";
        }




}

域类-

package com.nerdbot.student.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENT")
public class Student {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;



    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}

Jpa存储库-

package com.nerdbot.student.entity.repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.nerdbot.student.entity.Student;

public interface StudentRepo extends JpaRepository<Student, Long> {

}

h2 DB弹簧特性-

spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/spring-boot-h2-db
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update

如果你能指引我正确的方向,请告诉我


共 (2) 个答案

  1. # 1 楼答案

    我倾向于发现问题,只要我把它贴在这里:D,这就是我的问题得到解决的方式- 我返回了一个ResponseEntity对象,而不是String

     @RequestMapping(value="add", method = RequestMethod.POST)
            public  ResponseEntity<Void> add( @RequestBody Student input) {
    
              try {
                repo.save(input);
    
            } catch (Exception e) {
                System.err.println(e.getStackTrace());
                e.printStackTrace();
            }
    
              return ResponseEntity.ok().build();
    
            }
    

    删除所有请求也是如此

  2. # 2 楼答案

    我有以下创建新产品的方法:

    @PostMapping(value = "/products")
    public Product create(@RequestBody Product product) {
    
        return repo.save(product);
    }
    

    我在Postman中得到了404错误,因为在类lever注释中使用了@Controller。在我更改为@RestController之后,错误消失了,看起来一切正常