【发布时间】: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