Spring Boot 技术探索

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

7、Spring Boot中@RestController的使用

平台环境:

名称

版本号

Mac OS X

10.14.5

JDK

1.8.0_201

Apache Maven

3.6.0

IntelliJ IDEA

2019.1 (Ultimate Edition)

Spring Boot

2.1.6.RELEASE

 

  在Spring Boot 中,控制器Controller 负责处理访问请求,只需使用@Controller标记一个类是Controller ,然后使用@RequestMapping和@RequestParam等一些注解用以定义URL请求和Controller方法之间的映射,这样的Controller就能被外界访问到。

操作步骤:

1、创建model类

package com.example.demo.model;

public class User
{
    private String userName;
    private int age;
    private String remark;

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public String getRemark()
    {
        return remark;
    }

    public void setRemark(String remark)
    {
        this.remark = remark;
    }
}

 

2、创建Controller

这里发现一个技巧。只要在创建界面的类名称前边加上包名,就可以创建类的同时自动创建包名的文件夹。

 

代码:

package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

public class WebController
{
    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public User getUser()
    {
        User user = new User();
        user.setUserName("李雷");
        user.setAge(60);
        user.setRemark("Hello, I am LiLei");

        return user;
    }
}

 

3、浏览器访问http://localhost:8080/getUser

截图:

这里感受到了@RestController的功能如此强大,自动帮我们完成了从java类到JSON形式的转换。如果再增加一点难度,会如何呢?

 

4、修改WebController.java

package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class WebController
{
    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    public User getUser()
    {
        User user = new User();
        user.setUserName("李雷");
        user.setAge(60);
        user.setRemark("Hello, I am LiLei");

        return user;
    }

    @RequestMapping(value = "/getUser2", method = RequestMethod.POST)
    public List<User> getUser2()
    {
        User user = new User();
        user.setUserName("李雷");
        user.setAge(60);
        user.setRemark("Hello, I am LiLei");

        User user2 = new User();
        user2.setUserName("韩梅梅");
        user2.setAge(50);
        user2.setRemark("Hello, I am Hanmeimei");

        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user2);

        return list;
    }
}

 

5、浏览器访问http://localhost:8080/getUser2,返回结果如下:

[{"userName":"李雷","age":60,"remark":"Hello, I am LiLei"},{"userName":"韩梅梅","age":50,"remark":"Hello, I am Hanmeimei"}]

 

Bootstrap Thumbnail Second
MySQL

MySQL is the world's most popular open source database.

GO

Bootstrap Thumbnail Third
算法基础

本书介绍了什么是计算机算法,如何描述它们,以及如何来评估它们。

GO