Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (i) Quick start

Source: Internet
Author: User
Tags spring initializr

Quick Start
The main objective of this chapter is to build the Spring Boot Foundation project and implement a simple HTTP request processing, which gives a preliminary understanding of spring boot and experiences its simple structure and rapid development.

System Requirements:
Java 7 and above
Spring Framework 4.1.5 and above
This article uses Java 1.8.0_73, Spring Boot 1.3.2 Debugging.

Building projects with Maven
Generate basic projects with spring INITIALIZR tools
Visit: http://start.spring.io/
Choose the build tool Maven project, Spring boot version 1.3.2, and some engineering basics, as shown in the spring initializrspring INITIALIZR
Click Generate Project to download the item Compression pack
Unzip the project package and use the IDE to import the MAVEN project, taking IntelliJ idea 14 as an example:
menu, select File–>new–>project from Existing Sources ...
Select the extracted Items folder and click OK
Click Import Project from external model and select MAVEN and click Next to end.
If you have multiple versions of the JDK in your environment, please select Java SDK version 7
Project Structure Analysis
Project structure Project structure

The following steps complete the creation of the base project, as shown in the Spring boot infrastructure with a total of three files (the exact path varies according to the group all the differences that the user fills in when the project is generated):

Program entry under Src/main/java: Chapter1application
Configuration file under Src/main/resources: application.properties
Test entry under src/test/: chapter1applicationtests
Both the generated chapter1application and Chapter1applicationtests classes can run directly to start the currently created project, and because the project is currently not mated to any data access or Web modules, the program ends up running after the spring has finished loading.

Introducing the Web Module
The current pom.xml content is as follows, with only two modules introduced:
Spring-boot-starter: Core modules, including auto-configuration support, logs, and Yaml
Spring-boot-starter-test: Test modules, including JUnit, Hamcrest, Mockito

<dependencies>      <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter</artifactId>      </dependency>      <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-test</artifactId>          <scope>test</scope>      </dependency>  </dependencies>  

To introduce a Web module, add a spring-boot-starter-web module:

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

Writing HelloWorld Services
Create a package named Com.didispace.web (modified according to the actual situation)
Create the Hellocontroller class with the following content

@RestController  public class HelloController {        @RequestMapping("/hello")      public String index() {          return "Hello World";      }  }  

Start the main program, open the browser to access Http://localhost:8080/hello, you can see the page output Hello World
Writing Unit Test Cases
Open the src/test/test entry under the Chapter1applicationtests class. The following is a simple unit test to simulate an HTTP request, as follows:

@RunWith(SpringJUnit4Cla***unner.class)  @SpringApplicationConfiguration(classes = MockServletContext.class)  @WebAppConfiguration  public class Chapter1ApplicationTests {      private MockMvc mvc;      @Before      public void setUp() throws Exception {          mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();      }        @Test      public void getHello() throws Exception {          mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))                  .andExpect(status().isOk())                  .andExpect(content().string(equalTo("Hello World")));      }    }  

Use Mockservletcontext to build an empty webapplicationcontext, This allows the hellocontroller that we create to be created in the @before function and passed to the Mockmvcbuilders.standalonesetup () function.

Note introduce the following to make the status, content, Equalto functions available

import static org.hamcrest.Matchers.equalTo;  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;  

Now that the goal has been completed, a blank spring boot project has been built through MAVEN, and a simple request processing is implemented by introducing a Web module.
Source source technical support for complete projects 1791743380

Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (i) Quick start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.