java version of the e-commerce spring cloud distributed micro social service b2b2c electricity supplier (a) Quick Start

Quick Start
The main objective of this chapter to complete the construction of Spring Boot infrastructure projects, and to achieve a simple Http request processing, through this example has a basic understanding of Spring Boot, and its structure is simple, fast development of characteristic experience.

System requirements:
the Java 7 and above
Spring Framework 4.1.5 and above
this paper, Java 1.8.0_73, Spring Boot 1.3.2 debugging.

Use Maven to build projects
generated by project basis SPRING INITIALIZR tool
visit: http://start.spring.io/
choose to build tools Maven Project, Spring Boot version 1.3.2 as well as some basic project information, refer to the following SPRING INITIALIZR shown in Figure
Click Generate project Download the project archive
decompression program package, and use the IDE to import Maven projects to IntelliJ IDEA 14, for example:
menu, choose File-> New-> project from Existing Sources ...
select the project file is unpacked folder, click OK
click import project from external model and select Maven, click Next in the end so far.
If your environment has multiple versions of JDK, Java SDK noticed when selecting Please select the version more than 7 Java
project structure elucidation
project structure

Through the above steps to complete the creation of the base project, the infrastructure of a total of three Spring Boot file (specific path according to the user to fill in the project and all differences generated Group) as shown above:

src / main java program under / entrance: Chapter1Application
src / main / configuration files in resources: application.properties
test access src / test / under: Chapter1ApplicationTests
generated Chapter1Application and Chapter1ApplicationTests classes can run to start the project currently created Since the project is currently not cooperate with any data access or Web module, the program will end after the run finished loading Spring.

The introduction of Web module
current pom.xml as follows, only the introduction of two modules:

spring-boot-starter: core module, comprising a support automatic configuration, and logging YAML

<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>

The introduction of Web module, add spring-boot-starter-web module:


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

Write HelloWorld service
creation package named com.didispace.web (modified according to the actual situation)
to create HelloController class, as follows

@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 , see page output Hello World
to write unit test cases
test access Chapter1ApplicationTests class open src / test / under. Prepared following a simple unit test to simulate http request, as follows:


@RunWith(SpringJUnit4ClassRunner.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, so we created HelloController can be created and passed to MockMvcBuilders.standaloneSetup in @Before function (function).

Note the introduction of the following content, so that status, content, equalTo function 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;

The goal is now complete, by Maven build a blank Spring Boot project, and then implement a simple request processing by introducing a web module.
spring cloud b2b2c e-commerce social platform source code, please add penguin beg: three four five three six II qi II fifty-nine

Guess you like

Origin blog.csdn.net/qq_42748864/article/details/93709840