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

时间:2017-12-11 02:17:20

标签: java hibernate jpa spring-boot h2

我是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

请你指导我正确的方向。

2 个答案:

答案 0 :(得分:0)

我在这里发布时,我倾向于发现问题: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();

        }

同样可以删除所有请求。

答案 1 :(得分:0)

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

@PostMapping(value = "/products")
public Product create(@RequestBody Product product) {

    return repo.save(product);
}

我在 Postman 中收到 404 错误,因为在类杠杆注释中使用了 @Controller。更改为 @RestController 后,错误消失,一切正常。