On spring boot (i) HelloWorld

Source: Internet
Author: User

The setup process is as follows:

First, the Environment preparation:

    1. Install the JDK environment and search the Web tutorial yourself.

    2. Download and install MAVEN.

    3. Download and install the STS. Download URL: https://spring.io/tools/sts/all/

Second, build HelloWorld project

1. Select the Create Springboot Wizard

Path: file->new->spring Start Project

Select Web attributes, create a new Springboot project,

2. The following dependencies exist in the Pom.xml file.

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

3. Create a Helloworldcontroller class

@RestControllerpublic class Helloworldcontroller {@RequestMapping ("/hello") public String Index () {return '    Hello World "; }}

4. Run the Helloworldapplication class to start the service

5. Browser Input Http://127.0.0.1:8080/hello

6. Interface Output HelloWorld

The project was built successfully.


Third, testing

Create a test class to print execution results

@RunWith (springjunit4classrunner.class) @WebAppConfigurationpublic  class helloworldcontrolertests  {    private MockMvc mvc;     @Before      public void setup ()  throws Exception {         mvc = mockmvcbuilders.standalonesetup (New helloworldcontroller ()). Build ();     }     @Test     public void gethello ()   Throws exception {    mvc.perform (Mockmvcrequestbuilders.get ("/hello"). Accept ( Mediatype.application_json))                  .andexpect (Mockmvcresultmatchers.status (). IsOk ())                  .anddo (Mockmvcresulthandlers.print ())                  .andreturn ();     }} 

Result:

mockhttpservletrequest:      http method = get       request uri = /hello       parameters  = {}          headers = {accept=[ Application/json]}handler:             type  = com.example.demo.HelloWorldController            method = public java.lang.string com.example.demo.helloworldcontroller.index () Async:     async started = false     async result  = nullResolved Exception:              type = nullmodelandview:        view name  = null             view = null             Model = nullFlashMap:        Attributes = nullMockHttpServletResponse:            status = 200    error message = null           headers = {content-type=[application/ json;charset=iso-8859-1], content-length=[15]}     content type =  application/json;charset=ISO-8859-1              Body = Hello World LJZ    Forwarded URL =  null   redirected url = null           cookies = []


This article is from the "technical footprint" blog, so be sure to keep this source http://liangjingzhi.blog.51cto.com/13234473/1967970

On spring boot (i) HelloWorld

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.