繁体   English   中英

Spring MVC 转换 Spring Rest Api

[英]Spring MVC convert Spring Rest Api

我有一个 spring mvc 项目,我想转换 spring rest api 项目。

这是我的示例代码;

我的控制器

@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";
    }

}

我的 DAOImpl 类

    @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();

    }

}

其实我知道我需要改变的地方。 但我不太了解 spring Rest Api。 在我的代码中,我在视图(jsp 文件)中发送了许多属性,并在此处捕获了这些属性。 但是 RestApi 没有视图。 我需要删除属性函数。 但是我可以添加什么而不是属性函数? 我是新的 RestApi 请帮助我我该怎么办?

欢迎堆栈溢出。

Rest API 的工作方式如下:

  • 您公开一个端点和一个动词(例如 GET at /students)
  • 某些客户端调用您的端点(调用保存您的应用程序的服务器)
  • 您的服务器将调用委托给控制器函数(例如带有@GetMapping("/students")的函数)
  • 控制器函数向客户端发送响应(使用 spring,您的方法返回一个对象或一个 ResponseEntity)

REST API 接收请求,处理所述请求(以及请求数据,如果存在)并通常返回一些带有指示操作是否成功的状态代码的数据。

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

@GetMapping("/students")
ResponseEntity<List<Student>> listStudents() {
    List<Student> stds = getStudents(); // call some service or DB
    return new ResponseEntity<>(stds, HttpStatus.OK);
}

当您向 /students 发出 GET 请求时,spring 会将请求的处理委托给 listStudents 方法,该方法将获取学生并返回数据。

REST API 通常使用 JSON,因此您将返回的学生列表将被序列化为 json 列表。

如果要自定义学生 json 结构,可以使用 Jackson:

public class Student {
    @JsonProperty("cool_name") private String name;
// getters and setter
}

rest api 不适用于视图或 JSP。 它们通常处理 http 请求和响应。

如果您使用的是 spring mvc 而不是 spring boot,请查看这篇文章: https : //viralpatel.net/blogs/spring-4-mvc-rest-example-json/

如果您可以使用 spring boot(我强烈推荐),请检查: https : //spring.io/guides/gs/rest-service/

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

希望这会有所帮助并欢迎堆栈溢出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM