首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring转换Spring

Spring转换Spring
EN

Stack Overflow用户
提问于 2019-07-03 12:49:38
回答 1查看 1.3K关注 0票数 2

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

这里是我的样本代码;

我的控制器

代码语言:javascript
复制
@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课程

代码语言:javascript
复制
    @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();

    }

}

其实我知道我需要改变的地方。但我不太清楚春天的休息阿皮。在我的代码中,我在视图(jsp文件)中发送了许多属性,并在这里捕捉到这些属性。但是RestApi没有视图。我需要删除属性函数。但是我可以添加什么而不是属性函数呢?我是新来的RestApi,请帮帮我,我该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2019-07-03 13:12:16

欢迎来到堆栈溢出。

Rest的工作方式如下:

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

REST接收请求,处理所述请求(如果存在请求数据),并使用状态代码提示返回一些数据,该状态代码指示操作是否成功。

对于春天,你会做这样的事情:

代码语言:javascript
复制
@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使用JSON,因此您返回的学生列表将被序列化为json列表。

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

代码语言:javascript
复制
public class Student {
    @JsonProperty("cool_name") private String name;
// getters and setter
}

rest不适用于视图或JSP。他们巧妙地处理http请求和响应。

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

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

您还应该用@RestController对REST控制器进行注释,以便它们自动处理rest调用。

希望这有助于并欢迎堆栈溢出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56870559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档