Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Environment Building and RestFul style Interface in SpringBoot2.0

Shulou Source: shulou.com Published: 2022-06-02 11:26:56 10月16日 Update

Editor to share with you the SpringBoot2.0 environment building and RestFul style interface example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

1. Characteristics of SpringBoot framework 1. Characteristics of SpringBoot2.0

1) SpringBoot inherits the excellent gene of Spring, so it is easy to get started.

2) simplify configuration and provide various default configurations to simplify project configuration

3) embedded container simplifies Web project and coding

Spring Boot will help you start a web container quickly. In Spring Boot, you only need to add the following starter-web dependency to the pom file.

Org.springframework.boot spring-boot-starter-web

4) looking at the development trend

Micro-services are the trend of future development, and projects will slowly shift from traditional architecture to micro-service architecture, because micro-services can enable different teams to focus on a smaller range of job responsibilities, use independent technologies, and deploy more securely and more frequently.

2. Set up the environment of SpringBoot 1. Create a Maven project

2. Introduce core dependency org.springframework.boot spring-boot-starter-web3 and write configuration files.

Application.yml

# Port server: port: 80014, startup file annotation import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloApplication {public static void main (String [] args) {SpringApplication.run (HelloApplication.class,args);}}

No problem at all, just start the above class, and the basic environment for springboot is set up.

Think about the environment of the previous Spring framework, is this the feeling: understand it.

3. Several introductory cases of SpringBoot2.0 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;/** * SpringBoot2.0 first program * / @ RestControllerpublic class HelloController {@ RequestMapping ("/ getInfo") public ProjectInfo getInfo () {ProjectInfo info = new ProjectInfo (); info.setTitle ("SpringBoot2.0 basic tutorial") Info.setDate ("2019-06-05"); info.setAuthor ("smile"); return info;}}

The @ RestController annotation is equivalent to @ Controller + @ ResponseBody to return data in Json format.

2. Parameter mapping

1) first take a look at how SpringBoot distinguishes between environments

This identifies the configuration load specified configuration file.

2) Parameter configuration

Application-pro.yml

User: author: cicada title: SpringBoot 2.0 Program Development time: 2019-07-05

3) Parameter content reading

@ Componentpublic class ParamConfig {@ Value ("${user.author}") private String author; @ Value ("${user.title}") private String title; @ Value ("${user.time}") private String time; / / omit the get and set methods}

4) call mode

/ * * Environment configuration, parameter binding * / @ RestControllerpublic class ParamController {@ Resource private ParamConfig paramConfig; @ RequestMapping ("/ getParam") public String getParam () {return "[" + paramConfig.getAuthor () + ";" + paramConfig.getTitle () + ";" + paramConfig.getTime () + "]";}} 3, RestFul style interface and testing

1) Rest style API

/ * Rest style API test * / @ RestController / / equivalent @ Controller + @ ResponseBody returns Json format data @ RequestMapping ("rest") public class RestApiController {private static final Logger LOG = LoggerFactory.getLogger (RestApiController.class); / * * Save * / @ RequestMapping (value = "/ insert", method = RequestMethod.POST) public String insert (UserInfo userInfo) {LOG.info ("= = > >" + userInfo) Return "success";} / * query * / @ 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) @ WebAppConfigurationpublic class TestRestApi {private MockMvc mvc; @ Before public void setUp () throws Exception {mvc = MockMvcBuilders.standaloneSetup (new RestApiController ()). Build ();} / * * Test Save Interface * / @ Test public void testInsert () throws Exception {RequestBuilder request = null Request = post ("/ rest/insert/") .param ("id", "1") .param ("name", "test master") .param ("age", "20"); mvc.perform (request) .andexpect (content (). String (equalTo ("success") } / * Test query interface * / @ Test public void testSelect () throws Exception {RequestBuilder request = null; request = get ("/ rest/select/1"); mvc.perform (request) .andexpect (content () .string (equalTo ("success") }} these are all the contents of the article "example Analysis of Environment Building and RestFul style Interface in SpringBoot2.0". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Interfaces configurations environments tests styles parameters files projects content articles services examples analysis foundations containers data schemas formats frameworks comments Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Shulou Tech Info vpn Redmi