SpringBoot2.0 base case (01): RestFul environment to build and style interfaces

First, the characteristics SpringBoot framework

1, SpringBoot2.0 Features

1) SpringBoot inherited the good genes Spring, little difficult to get started
2) simplify the configuration, providing a variety of default configuration simplifies project configuration
3) Embedded Web container to simplify the project, simplify the coding
Spring Boot will help develop a web with quick start vessel, Spring Boot, only need to add the following to a starter-web in dependence pom file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

4) the development trend
of micro service is the future trend of development, the project will gradually shift from the traditional architecture of the micro-service architecture, since the micro-services enable different teams to focus on a smaller range of job duties, using a separate technology, safer and more frequent deployments.

Second, build SpringBoot environment

1, create a Maven project

SpringBoot2.0 base case (01): RestFul environment to build and style interfaces

2, dependent on Core

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3, write a configuration file

application.yml

# 端口
server:
  port: 8001

4, the startup file notes

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args) ;
    }
}

And no problem, so be it start above this class, springboot the basis of environmental build better.
Think about the environment before the Spring framework to build, is not that this feeling: sense about it.

Three, SpringBoot2.0 several cases entry

1, create a Web Interface

import com.boot.hello.entity.ProjectInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * SpringBoot 2.0 第一个程序
 */
@RestController
public class HelloController {
    @RequestMapping("/getInfo")
    public ProjectInfo getInfo (){
        ProjectInfo info = new ProjectInfo() ;
        info.setTitle("SpringBoot 2.0 基础教程");
        info.setDate("2019-06-05");
        info.setAuthor("知了一笑");
        return info ;
    }
}

@RestController annotation @Controller + @ResponseBody return Json equivalent format data.

2, parameter mapping

1) First, look at how to distinguish between environment SpringBoot
SpringBoot2.0 base case (01): RestFul environment to build and style interfaces

Here identity is configured to load the specified configuration file.

2) parameter configuration
application-pro.yml

user:
  author: 知了一笑
  title: SpringBoot 2.0 程序开发
  time: 2019-07-05

3) reading the parameter content

@Component
public class ParamConfig {
    @Value("${user.author}")
    private String author ;
    @Value("${user.title}")
    private String title ;
    @Value("${user.time}")
    private String time ;
    // 省略 get 和 set 方法
}

4) invocation

/**
 * 环境配置,参数绑定
 */
@RestController
public class ParamController {

    @Resource
    private ParamConfig paramConfig ;

    @RequestMapping("/getParam")
    public String getParam (){
        return "["+paramConfig.getAuthor()+";"+
                paramConfig.getTitle()+";"+
                paramConfig.getTime()+"]" ;
    }
}

3, RestFul style interfaces and test

1) Rest style interfaces

/**
 * Rest 风格接口测试
 */
@RestController // 等价 @Controller + @ResponseBody 返回Json格式数据
@RequestMapping("rest")
public class RestApiController {
    private static final Logger LOG = LoggerFactory.getLogger(RestApiController.class) ;
    /**
     * 保存
     */
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insert (UserInfo userInfo){
        LOG.info("===>>"+userInfo);
        return "success" ;
    }
    /**
     * 查询
     */
    @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
    public String select (@PathVariable Integer id){
        LOG.info("===>>"+id);
        return "success" ;
    }
}

2) test code

@RunWith(SpringJUnit4Cla***unner.class)
@SpringBootTest(classes = MockServletContext.class)
@WebAppConfiguration
public class TestRestApi {

    private MockMvc mvc;
    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new RestApiController()).build();
    }

    /**
     * 测试保存接口
     */
    @Test
    public void testInsert () throws Exception {
        RequestBuilder request = null;
        request = post("/rest/insert/")
                .param("id", "1")
                .param("name", "测试大师")
                .param("age", "20");
        mvc.perform(request)
                .andExpect(content().string(equalTo("success")));
    }

    /**
     * 测试查询接口
     */
    @Test
    public void testSelect () throws Exception {
        RequestBuilder request = null;
        request = get("/rest/select/1");
        mvc.perform(request)
                .andExpect(content().string(equalTo("success")));
    }
}

Such SpringBoot2.0 entry case is over, simple, elegant in style.

Fourth, the source code

51CTO

GitHub地址:知了一笑
https://github.com/cicadasmile
码云地址:知了一笑
https://gitee.com/cicadasmile

SpringBoot2.0 base case (01): RestFul environment to build and style interfaces
SpringBoot2.0 base case (01): RestFul environment to build and style interfaces

Guess you like

Origin blog.51cto.com/14439672/2427531