【问题标题】:Data is getting saved and deleted even after 404 error in spring-boot and H2即使在 spring-boot 和 H2 出现 404 错误后,数据也会被保存和删除
【发布时间】:2018-05-24 13:03:13
【问题描述】:

我是 H2 和 Spring-boot 组合的新手,我正在使用所有最新更新来创建一个简单的 rest-API,我已经创建了多个端点来在我的控制器类中操作和呈现数据,但每当我发布一个新对象我在邮递员中收到 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 属性 -

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

请您指导我正确的方向。

【问题讨论】:

    标签: java hibernate jpa spring-boot h2


    【解决方案1】:

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

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

    我在 Postman 中收到 404 错误,因为在类杠杆注释中使用了 @Controller。在我更改为@RestController 后,错误消失了,一切看起来都很好。

    【讨论】:

      【解决方案2】:

      我倾向于在将问题发布到此处后立即找到问题: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();
      
              }
      

      删除所有请求也是如此。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2019-06-19
        • 2016-05-17
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 2021-12-02
        相关资源
        最近更新 更多