SpringMVC on RESTful architectural style Profile

REST: Representational State Transfer (Representational State Transfer)
REST is not an innovative technology, which refers to a set of architectural constraints and principles. Subject to the constraints and principles of REST architecture, call it RESTful architecture.

RESTful core
 1, resources and URI of the
      REST is Representational State Transfer, is that really mean what representations it? In fact, the expression refers to the resources
What are your resources? Any one transaction in the system, referenced as long as necessary, then it is a resource, it can be a piece of text, which can be a picture, or even a service, in short, there is a real resource. To want to be identified resources, we need to have a unique identifier, on the Web, this logo is the URI, in fact, address resource

2, of the resource are
     the resource representation is transferred resources between the client and the server
      
3, the state transition
      resources for transmitting changes in the client, and then goes to a subsequent state.

RESTful architectural features

  • Unified client interface to access resources
  • url more concise, easy to understand, easy to expand
  • Conducive to the sharing of resources between different systems

Specifically, four forms is RESTful HTTP protocol indicates four basic operations

  • GET: access to resources
  •  POST: New Resource
  • PUT: Modify Resources
  • DELETE: Delete Resource


RESTful development style
queries course: http: // localhost: 8080 / id method = 'get'
add courses: http: // localhost: 8080 / course method = 'post'
Modify course: http: // localhost: 8080 / id method = 'put'
delete courses: http: // localhost: 8080 /  id method = 'delete'

The following are examples

Entity classes: Course

package com.lzy.entity;

public class Course {
    private int id;
    private String name;
    private double price;

    public int getId(){
        return id;
    }
    public void setId(int id){
        this.id=id;
    }

    public String getName(){
        return name;
    }
    public void setName(){
        this.name=name;
    }
    public double getPrice(){
        return  price;
    }
    public void setPrice(double price){
        this.price=price;
    }

}

dao layer

package com.lzy.dao;

import com.lzy.entity.Course;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class CourseDao {
    private Map<Integer, Course>courses = new HashMap<Integer, Course>();

    /**
     * 新增课程
     */
    public void add(Course course){
        courses.put(course.getId(),course);
    }

    /**
     * 查询全部课程
     */
    public Collection<Course>getAll(){
        return courses.values();
    }

    /**
     * 通过id查询课程
     */
    public Course getById(int id){
        return courses.get(id);
    }

    /**
     * 修改课程
     */
    public void update(Course course){
        courses.put(course.getId(),course);
    }

    /**
     * 删除课程
     */
    public void deleteById(int id){
        courses.remove(id);
    }
}

Control Layer

package com.lzy.controller;

import com.lzy.dao.CourseDao;
import com.lzy.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
public class CourseController {
    @Autowired
    private CourseDao courseDao;

    /**
     * 添加课程
     */
    @PostMapping(value = "/add")
    public String add(Course course){
        courseDao.add(course);
        return "redirect:/getAll";
    }


    /**
     * 查询全部课程
     */
    @GetMapping(value = "/getAll")
    public ModelAndView getAll(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("courses",courseDao.getAll());
        return modelAndView;
    }

    /**
     * 通过id查询课程
     */
    @GetMapping(value = "/getById/{id}")
    public ModelAndView getById(@PathVariable(value = "id")int id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("edit");
        modelAndView.addObject("course",courseDao.getById(id));
        return modelAndView;
    }

    /**
     * 修改课程
     */
    @PutMapping(value = "/update")
    public String update(Course course){
        courseDao.update(course);
        return "redirect:/getAll";
    }

    /**
     * 删除课程
     */
    @DeleteMapping(value = "/delete/{id}")
    public String delete(@PathVariable(value = "id") int id){
        courseDao.deleteById(id);
        return "redirect:/getAll";
    }
}

 

Guess you like

Origin blog.csdn.net/qq_41937388/article/details/90690622