Spring Boot 2.0 bis new RESTfull style project

1. Create a new Maven project (for specific methods, please refer to one of SpringBoot)

2. First create a User class

package com.somta.springboot.pojo;
public class User {

private String id;

private String name;//姓名

private Integer age; // age   

    // setter and getter methods omitted 

}

3. Create a new UserController class and write some restfull interfaces

@RestController

public class UserController {

   @GetMapping("/getString")
    public String getString() {
        return "Hello 张三";
    }

   @GetMapping("/queryUserById")
    public User queryUserById() {
        User user = new User();
        user.setId("123456");
        user.setName( "Zhang San" );
        user.setAge(12);
        return user;
    }

    @GetMapping("/queryUserList")
    public List<User> queryUserList() {
        List<User> list = new ArrayList<>();

            User user = new User();
            user.setId("123456");
            user.setName( "Zhang San" );
            user.setAge(12);

            User user2 = new User();
            user2.setId("789");
            user2.setName( "Li Si" );
            user2.setAge(22);

           list.add(user);
           list.add(user2);
           return list;
    }

}   

SpringBoot advocates the use of simpler methods and annotations to develop programs

1. @RestController annotation When we look at its source code, we can see that it is composed of @Controller and @RsponseBody, so that we don't need to specify @RsponseBody one by one, so that the returned object can be converted into a json object and returned.

2. @GetMapping() This is another abbreviated @RequestMapping(value = "/queryUserById", method=RequestMethod.GET), annotated with this annotation are @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @ PatchMapping greatly simplifies the mapping of our commonly used HTTP methods.

4. Enter the request address on the browser, and you can view the following page, indicating that you have succeeded!

Git code address: https://gitee.com/songhu/SpringBoot/tree/master/SpringBoot-restfull

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325205012&siteId=291194637