Principle analysis of SpringMVC framework REST architecture

  • 2021-11-14 05:33:31
  • OfStack

Directory resources (Resource) Presentation layer (Representation) State transition (State Transfer) How to use 1. Write the method of adding, deleting and modifying in Handler 2. Repository

Resources (Resource)

A resource is an entity on the network, or a specific information, a paragraph, a picture, a song, a video, etc. existing in the network, in short, it is a specific existence. You can point to it with an URI (unified resource locator), and each resource has a corresponding specific URI. To obtain this resource, you only need to access the corresponding URI.

Appearance layer (Representation)

The specific forms of resources can be expressed in txt format, HTML, XML, JSON and other formats.

State Transition (State Transfer)

If the client wishes to manipulate a resource in the server, it needs to make the server make a state transition in some way, and this transition is built on the presentation layer, which is called "presentation layer state transition"

Advantages of Rest: URL is more concise. It is beneficial to resource sharing between different systems, and resource sharing can be realized only by complying with certain specifications and without other configurations

How to use

How to make REST specific operation is that the four verbs indicating operation mode in HTTP protocol correspond to CRUD basic operation respectively. GET is used to denote getting resources. POST is used to represent a new resource. PUT is used to represent modified resources. DELETE is used to indicate the deletion of resources.

1. Write the method of adding, deleting and modifying in Handler


package Mycontroller;
import entity.Student;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import repository.StudentRepository;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
@RequestMapping("/rest")
@RestController
public class RestHandler {
    @Autowired
    private StudentRepository studentRepository;
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
//    @GetMapping("/findAll")
    public Collection<Student> findAll(HttpServletResponse response){
        response.setContentType("text/json;charset=UTF-8");
        return studentRepository.findAll();
    }
    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id") long id)
    {
        return studentRepository.findById(id);
    }
    @PostMapping("/sava")
    public void save(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.saveOrUpdate(student);
    }
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
        studentRepository.deleteById(id);
    }
}

2.Repository


@Repository
public class StudentRepositoryImpl implements StudentRepository {
    private static Map<Long, Student> studentMap;
    static {
        studentMap=new HashMap<>();
        studentMap.put(1L,new Student (1L,"zhangsan",22));
    }
    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }
    @Override
    public Student findById(long id) {
        return studentMap.get(id);
    }
    @Override
    public void saveOrUpdate(Student student) {
        studentMap.put(student.getId(),student);
    }
    @Override
    public void deleteById(long id) {
        studentMap.remove(id);
    }
}

The above is the SpringMVC framework REST framework brief analysis of the details, more information about SpringMVC framework REST framework please pay attention to other related articles on this site!


Related articles: