Spring Boot Quick Start

Source: Internet
Author: User

Introducing the Web Module

The current pom.xml content is as follows, with only two modules introduced:

    • spring-boot-starter: core modules, including automatic 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>
Span class= "line" ></DEPENDENCY>
</DEPENDENCIES>

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

<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
Writing HelloWorld Services
    • Create package a named com.didispace.web (modified according to the actual situation)
    • Create HelloController a class with the following content
@RestController
Hellocontroller {

@RequestMapping ("/hello")
Index() {
"Hello World";
}

}
    • Start the main program, open the browser access http://localhost:8080/hello , you can see the page outputHello World
Writing Unit Test Cases

Open the src/test/ test entry class under Chapter1ApplicationTests . The following is a simple unit test to simulate an HTTP request, as follows:

@RunWith (Springjunit4classrunner.class)
@SpringApplicationConfiguration (classes = mockservletcontext.class)
@WebAppConfiguration
PublicClasschapter1applicationtests {

Private MOCKMVC MVC;

@Before
PublicvoidSetUp () 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"));

/span>

Use MockServletContext to build an empty one WebApplicationContext so that we can create HelloController it @Before and pass it to the function in the function MockMvcBuilders.standaloneSetup() .

    • Note that the following content is introduced to make, status content and equalTo function available
Static org.hamcrest.Matchers.equalTo;
Static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
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.

Spring Boot 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.