Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report