美文网首页
01-构建一个springboot工程

01-构建一个springboot工程

作者: stellarxiong | 来源:发表于2017-12-27 19:40 被阅读0次

    构建过程

    打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。

    目录结构入下:

    ├─.idea
    │  └─inspectionProfiles
    ├─.mvn
    │  └─wrapper
    └─src
        ├─main
        │  ├─java
        │  │  └─com
        │  │      └─stellar
        │  │          └─springboot
        │  │              └─springbootfirstapplication #程序入口
        │  └─resources
        │      ├─static #静态资源
        │      └─templates #模板资源
        |      └─application.yml #配置文件
        └─test
            └─java
                └─com
                    └─stellar
                        └─springboot
                            └─springbootfirstapplication
    └─pom.xml
    

    pom文件如下

    ?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.stellar.springboot</groupId>
        <artifactId>springboot-first-application</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>springboot-first-application</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <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>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    

    spring-boot-starter-web的jar中包含了spring mvc相关的组件及默认配置,还有各种组件的自动配置,因此在此种不需要配置项目通过springbootfirstapplication入口可以直接启动。

    添加controller访问

    在启动入口做如下修改:

    @RestController //配置返回JSON格式
    @SpringBootApplication
    public class SpringbootFirstApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootFirstApplication.class, args);
        }
        //添加一个controller方法
        @RequestMapping("/")
        public String helloTest(){
            return "This is the first springboot application";
        }
    }
    

    通过如可main方法启动服务,调用请求http://localhost:8080/返回结果如下:

    This is the first springboot application
    
    • 此时tomcat使用的是内置的tomcat
    • web.xml和springmvcd 配置用的是默认配置

    查看加载了哪些类

    在入口类中添加如下代码,打印启动加载了哪些类:

        @Bean
        public CommandLineRunner commandLineRunner(ApplicationContext ctx){
            return args -> {
                System.out.println("Let's inspect the beans provided by Spring Boot:");
                //启动打印加载的类
                String[] beanNames = ctx.getBeanDefinitionNames();
                Arrays.sort(beanNames);
                for (String beanName : beanNames) {
                    System.out.println(beanName);
                }
            };
        }
    

    单元测试代码入下

    方式一:

    package com.stellar.springboot.springbootfirstapplication;
    
    import org.hamcrest.Matchers;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.embedded.LocalServerPort;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.web.client.TestRestTemplate;
    import org.springframework.http.ResponseEntity;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.net.MalformedURLException;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)//需要配置webEnviroment要不会报错
    public class SpringbootFirstApplicationTests {
    
        @LocalServerPort
        private int port;
    
        private String baseUrl;
    
        @Autowired
        private TestRestTemplate testRestTemplate;
    
        @Before
        public void setUp() throws MalformedURLException {
            this.baseUrl="http://localhost:"+this.port+"/";
        }
    
        @Test
        public void testHello(){
            ResponseEntity<String> responseEntity = this.testRestTemplate.getForEntity(this.baseUrl,String.class);
            Assert.assertThat(responseEntity.getBody(), Matchers.equalTo("This is the first springboot application"));
        }
    }
    

    方式二:

    import org.hamcrest.Matchers;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloTest {
        @Autowired
        private MockMvc mvc;
    
        @Test
        public void testHello() throws Exception {
            this.mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)
            ).andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.content().string(Matchers.equalTo("This is the first springboot application")));
        }
    
    }
    

    来源:http://blog.csdn.net/forezp/article/details/70341651

    相关文章

      网友评论

          本文标题:01-构建一个springboot工程

          本文链接:https://www.haomeiwen.com/subject/accpgxtx.html