• 记录我的第一个Spring Boot


    Spring Boot的guides有推荐的建立spring项目的插件,但是我现在不打算用

    使用工具

    1. myeclipse
    2. myeclpse自带maven(3.3.3)

    成功页面:

    建立过程:

    pom.xml

    <?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.liantao</groupId>
        <artifactId>spring-boot</artifactId>
        <version>0.1.0</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
             <version>1.5.10.RELEASE</version>
            <relativePath/>
        </parent>
    
        <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>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    在src/main/java/hello下新建HelloController.java

    package hello;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/")
        public String index() {
            return "Greetings from Spring Boot!我的第一个Spring Boot.";
        }
    
    }
    

    该类被标记为 @RestController,这意味着Spring MVC可以使用它来处理Web请求。@RequestMapping映射/index()方法。从浏览器调用或在命令行上使用curl时,该方法返回纯文本。这是因为@RestController组合@Controller@ResponseBody两个注释会导致Web请求返回数据而不是视图。

    在src/main/java/hello下新建Application.java

    package hello;
    
    import java.util.Arrays;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @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);
                }
    
            };
        }
    
    }
    

    @SpringBootApplication 是一个便利注释,添加了以下所有内容:

    • @Configuration 标记该类作为应用程序上下文的bean定义的源。

    • @EnableAutoConfiguration 告诉Spring Boot开始根据类路径设置,其他bean和各种属性设置添加bean。

    • 通常你会添加@EnableWebMvc一个Spring MVC应用程序,但Spring Boot会在类路径上看到spring-webmvc时自动添加它。这会将应用程序标记为Web应用程序并激活关键行为,例如设置 DispatcherServlet

    • @ComponentScan告诉Spring在包中寻找其他组件,配置和服务hello,允许它找到控制器。

    main()方法使用Spring Boot的SpringApplication.run()方法来启动应用程序。您是否注意到没有一行XML?也没有web.xml文件。此Web应用程序是100%纯Java,您无需处理配置任何管道或基础结构。

    还有一个CommandLineRunner标记为@Bean的方法,它在启动时运行。它检索由您的应用程序创建或由于Spring Boot自动添加的所有bean。它对它们进行分类并打印出来。


    添加单元测试

    1. 在pom.xml中添加
     <!-- 添加单元注释 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
    1. 在src/test/java/hello下添加HelloControllerTest.java
    package hello;
    
    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;
    
    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;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloControllerTest {
    
        @Autowired
        private MockMvc mvc;
    
        @Test
        public void getHello() throws Exception {
            mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                    .andExpect(status().isOk())
                    .andExpect(content().string(equalTo("Greetings from Spring Boot!我的第一个Spring Boot.")));
        }
    }
    

    Run As Junit Test 无报错即成功。

    以上配置导致pom.xml第一行报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration) pom.xml/

    虽然并不影响结果,但是,不喜欢的话,解决办法:
    pom.xml中:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    

    改为

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.10.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    

    END

  • 相关阅读:
    jsp类的封装集合的应用及servlet的引入和JSTL的引入
    jsp学习1
    小型世界关系图的交互可视化(未完成)
    c++产生随机数问题
    socket error
    opengl空间画圆柱体
    socket连接服务器立即返回,不用三次握手
    win7电脑分无线网,简称热点
    设置ipv6全球地址和默认网关
    网线制作,水晶头里的线顺序
  • 原文地址:https://www.cnblogs.com/famine/p/10064048.html
Copyright © 2020-2023  润新知