Java SpringMVC转换SpringRESTAPI

Java SpringMVC转换SpringRESTAPI,java,spring-boot,spring-mvc,jpa,Java,Spring Boot,Spring Mvc,Jpa,我有一个SpringMVC项目,我想转换SpringRESTAPI项目 这里是我的示例代码 我的控制器 @Controller @RequestMapping("/student") public class StudentController { @Autowired private StudentService studentService; @Autowired private SchoolService schoolService; @GetM

我有一个SpringMVC项目,我想转换SpringRESTAPI项目

这里是我的示例代码

我的控制器

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

    @Autowired
    private StudentService studentService;

    @Autowired
    private SchoolService schoolService;
    @GetMapping("/list")
    public String ListStudents(Model model) {

        List<Student> theStudent = studentService.getStudents();
        model.addAttribute("students", theStudent);

        return "List-student";
    }

    @GetMapping("/addNewStudent")
    public String addNewStudent(Model model) {

        Student theStudent = new Student();
        model.addAttribute("student", theStudent);

        List<School> theSchool = schoolService.getSchools();
        model.addAttribute("schools", theSchool);
        return "student-form";
    }

    @PostMapping("/saveStudent")
    public String saveStudent(@ModelAttribute("student") Student theStudent) {

        studentService.saveStudent(theStudent);

        return "redirect:/student/list";
    }

    @GetMapping("/showFormforUpdate")
    public String showFormForUpdate(@RequestParam("studentID") int theId, Model model) {

        Student theStudent = studentService.getStudent(theId);
        model.addAttribute(theStudent);
        List<School> theSchool = schoolService.getSchools();
        model.addAttribute("schools", theSchool);
        return "student-form";

    }

    @GetMapping("/deleteStudent")
    public String deleteStudent(@RequestParam("studentID") int theId, Model model) {

        studentService.deleteStudent(theId);

        return "redirect:/student/list";
    }

}
@控制器
@请求映射(“/student”)
公共班级学生控制员{
@自动连线
私人学生服务学生服务;
@自动连线
私立学校服务学校服务;
@GetMapping(“/list”)
公共字符串列表学生(模型){
列出student=studentService.getStudents();
添加属性(“学生”,即学生);
返回“学生名单”;
}
@GetMapping(“/addNewStudent”)
公共字符串addNewStudent(模型){
学生学生=新学生();
添加属性(“学生”,即学生);
列出school=schoolService.getSchools();
model.addAttribute(“学校”,学校);
返回“学生表格”;
}
@后期映射(“/saveStudent”)
公共字符串saveStudent(@modeldattribute(“student”)student-theStudent){
学生服务。保存学生(学生);
返回“重定向:/student/list”;
}
@GetMapping(“/showFormforUpdate”)
公共字符串showFormForUpdate(@RequestParam(“studentID”)int theId,Model){
Student theStudent=studentService.getStudent(theId);
model.addAttribute(学生);
列出school=schoolService.getSchools();
model.addAttribute(“学校”,学校);
返回“学生表格”;
}
@GetMapping(“/deleteStudent”)
公共字符串deleteStustudent(@RequestParam(“studentID”)int theId,Model){
学生服务。删除学生(theId);
返回“重定向:/student/list”;
}
}
我的课堂

    @Repository
public class StudenDAOImpl implements StudentDAO {

    @Autowired
    private SessionFactory sessionFactory;


    @Override
    @Transactional
    public List<Student> getStudents() {

        Session currentSession=sessionFactory.getCurrentSession();
        Query<Student> theQuery = currentSession.createQuery("from Student", Student.class);
        List<Student> students=theQuery.getResultList();
        return students;
    }


    @Override
    public void saveStudent(Student theStudent) {

        Session currentSession=sessionFactory.getCurrentSession();
        currentSession.saveOrUpdate(theStudent);
    }


    @Override
    public Student getStudent(int theId) {

        Session currentSession=sessionFactory.getCurrentSession();
        Student theStudent=currentSession.get(Student.class, theId);

        return theStudent;
    }


    @Override
    public void deleteStudent(int theId) {
        Session currentSession=sessionFactory.getCurrentSession();
        Query theQuery=currentSession.createQuery("delete from Student where id=:studentID");
        theQuery.setParameter("studentID", theId);
        theQuery.executeUpdate();

    }

}
@存储库
公共类StudenDAOImpl实现StudentDAO{
@自动连线
私人会话工厂会话工厂;
@凌驾
@交易的
公众学生名单{
会话currentSession=sessionFactory.getCurrentSession();
Query theQuery=currentSession.createQuery(“来自学生”,学生,班级);
List students=theQuery.getResultList();
留学生;
}
@凌驾
公共无效保存学生(学生学生){
会话currentSession=sessionFactory.getCurrentSession();
当前会话。保存或更新(学生);
}
@凌驾
公立学生getStudent(int theId){
会话currentSession=sessionFactory.getCurrentSession();
学生theStudent=currentSession.get(Student.class,theId);
返回学生;
}
@凌驾
公共学生(int theId){
会话currentSession=sessionFactory.getCurrentSession();
Query theQuery=currentSession.createQuery(“从学生中删除,其中id=:studentID”);
setParameter(“studentID”,theId);
executeUpdate();
}
}

事实上,我知道我需要改变的地方。但是我不太了解SpringRESTAPI。在我的代码中,我在视图(jsp文件)中发送了许多属性,我在这里捕获这些属性。但RestApi没有观点。我需要删除属性函数。但是我可以添加什么来代替属性函数呢?我是新来的。请帮我做什么?

欢迎来到堆栈溢出

Rest API的工作原理如下:

  • 公开端点和动词(例如GET at/students)
  • 某些客户端调用您的端点(调用保存您的应用程序的服务器)
  • 服务器将调用委托给控制器函数(例如带有
    @GetMapping(“/students”)
    的函数)
  • 控制器函数向客户端发送响应(使用spring,您的方法返回一个对象或ResponseEntity)
RESTAPI接收请求,处理所述请求(以及请求数据,如果存在),并返回一些数据,其中包含一个状态代码,指示操作是否成功

使用spring,您可以执行以下操作:

@GetMapping("/students")
ResponseEntity<List<Student>> listStudents() {
    List<Student> stds = getStudents(); // call some service or DB
    return new ResponseEntity<>(stds, HttpStatus.OK);
}
RESTAPI不适用于视图或JSP。它们通常处理http请求和响应

如果您使用的是spring mvc而不是spring boot,请查看本文:

如果您可以使用spring boot(我强烈推荐),请检查以下内容:

您还应该使用
@RestController
注释REST控制器,以便它们自动处理REST调用


希望这有帮助,欢迎来到stack overflow

如果您想获得详细的指南,请参阅我的书(免责声明:我是作者)谢谢我的朋友您给了我宝贵的信息,我一定会这样做。我想再问一件事。我的项目中有两个实体类。在SpringMVC中,我用2个xml文件连接了我的sql数据库。xml和spring-servlet.xml。spring boot中的xml文件替换了什么?我发现它是application.properties文件中的一个。但是其他文件有什么作用呢?再次感谢您的帮助:)购买我与STS合作的方式。在我的SpringMVC项目中,我有一个名为WebContent的文件夹,这个文件夹中有两个xml文件。但是在我的SpringBootRESTAPI项目中,我没有这个文件夹。我有一个名为application.properties的文件。我很困惑我该怎么办。我需要定义我的控制器和服务类。但是在哪里呢?
public class Student {
    @JsonProperty("cool_name") private String name;
// getters and setter
}