【问题标题】:Spring MVC convert Spring Rest ApiSpring MVC 转换 Spring Rest Api
【发布时间】:2019-11-14 04:04:19
【问题描述】:

我有一个 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 请帮助我该怎么办?

【问题讨论】:

标签: java spring-boot spring-mvc jpa


【解决方案1】:

欢迎堆栈溢出。

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 控制器,以便它们自动处理休息调用。

希望对您有所帮助,欢迎堆栈溢出

【讨论】:

  • 谢谢我的朋友,你给了我宝贵的信息,我一定会做到的。我还想问一件事。我的项目中有 2 个实体类。在 spring mvc 中,我将我的 sql 数据库与 2 个 xml 文件连接起来。 Web.xml 和 spring-servlet.xml。什么会在 Spring Boot 中替换这个 xml 文件?我发现它是其中一个 application.properties 文件。但是其他文件呢?再次感谢您的帮助:)
  • 购买我使用 STS 的方式。在我的 spring mvc 项目中,我有一个名为 Web-Content 的文件夹,这个文件夹中有 2 个 xml 文件。但是在我的 Spring Boot Rest Api 项目中,我没有这个文件夹。我有一个名为 application.properties 的文件。我很困惑我该怎么办。我需要定义我的控制器和服务类。但是在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
相关资源
最近更新 更多