Create the project and partition the modules corresponding to the package

  • Create a project
  • Configuring startup Classes
  • Create corresponding packages, first create these packages, other use later supplement
    • controller
    • service
    • dao(mapper)
    • domain
    • utils
  • Initial data development: video, chapter, default user information

Request the actual interface

A GET request

@getMapping = @requestMethod.get (method = requestmethod.get)

  • Combination of notes
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
Copy the code

The operation to develop a list of queries

  • Domain creates entity classes that give corresponding properties and methods
public class Video {
    private int id;
    private String title;
    private String summary;
    private int price;
    private String coverImg;
    private Date createTime;

    public Video(a) {}public Video(int id, String title) {
        this.id = id;
        this.title = title;
        this.createTime=new Date();
    }
Copy the code
  • Simulate a database in Mapper and perform operations to return data
@Repository
public class VideoMapper {
    // Simulate database data
    private static Map<String, Video> videoMap = new HashMap<>();
    static {
        videoMap.put("1".new Video(1."Gigi Birch."));
        videoMap.put("2".new Video(2."Phil"));
        videoMap.put("3".new Video(3."jjbq"));
        videoMap.put("4".new Video(4."junko"));
        videoMap.put("5".new Video(5."La la la la"));
    }
    // Request to query all data
    public List<Video> findAllVideo(a){
        List<Video> videoList = new ArrayList<>();
        videoList.addAll(videoMap.values());
        returnvideoList; }}Copy the code
  • Service creates interfaces and implementation classes
/ / interface
public interface VideoService {

    public List<Video> findAll(a);
}

/ / implementation class
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<Video> findAll(a) {
        List<Video> allVideo = videoMapper.findAllVideo();
        returnallVideo; }}Copy the code
  • Controller calls service
@RestController
@RequestMapping("api/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @RequestMapping("findAll")
    public List<Video> findAllVideo(a){
        List<Video> all = videoService.findAll();
        returnall; }}Copy the code

Obtain data in Postman and display it successfully

Improved thinking in the request interface

Status code

We can realize the return of data above, but when connecting with the front end, should we give a status code to mark whether the data is successfully returned? Unified protocol

Custom return object, unified protocol

  • Create and use the JSONData utility class under the Utils package
public class JsonData {
    private int code;
    private Object data;
    private String msg;

    public static JsonData buildSuccess(Object data){
        return new JsonData(0,data);
    }

    public static JsonData buildError(String msg){
        return new JsonData(-1."",msg);
    }

    public static JsonData buildError(String msg,int code){
        return new JsonData(code,"",msg);
    }
Copy the code
  • And then you just need to call it in a method in the Controller
@RequestMapping("findAll2")
    public Object findAllVideo2(a){
        List<Video> all = videoService.findAll();
        return JsonData.buildSuccess(all);
    }
Copy the code

POST Verifies the user name and password

HTTP POST request

  • POST request-form form
    • Scenario: Form submission, POST if you don’t subdivide PUT/DELETE methods
    • PostMapping = @requestMapping (method = requestmethod.post)
  • Development function: account password submission login interface, form form

Develop the login operation

  • domain
public class User {
    private int id;
    private String username;
    private String password;
Copy the code
  • mapper
@Repository
public class UserMapper {
    private static Map<String, User> userMap = new HashMap<>();

    static {
        userMap.put("jjbq".new User(1."jjbq"."123123"));
        userMap.put("junko".new User(2."junko"."111111"));
        userMap.put("hhh".new User(3."hhh"."123"));
    }

    public User login(String username,String password){
        User user = userMap.get(username);
        if (user == null) {return null;
        }
        if (user.getPassword().equals(password)){
            return user;
        }
        return null; }}Copy the code
  • service
public interface UserService {
    public String login(String username,String password);
}

@Service
public class UserServiceImpl implements UserService {

    private static Map<String,User> sessionMap = new HashMap<>();

    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(String username,String password ) {
        User user = userMapper.login(username,password);
        if (user==null) {return null;
        }else {
            String token = UUID.randomUUID().toString();
            sessionMap.put(token,user);
            return user.toString()+":"+token; }}}Copy the code
  • controller
@RestController
@RequestMapping("api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public JsonData login(@RequestBody User user){
        String login = userService.login(user.getUsername(), user.getPassword());
        if (StringUtils.isEmpty(login)){
            return JsonData.buildError("Username and password error");
        }else {
            returnJsonData.buildSuccess(login); }}}Copy the code