发表于: 2018-09-10 22:59:44

1 353


Spring Boot 的第一个Demo:

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

目录结构如下:

  • pom文件为基本的依赖管理文件
  • resouces 资源文件 
    • statics 静态资源
    • templates 模板资源
    • application.yml 配置文件
  • SpringbootApplication程序的入口。

来看配置文件:

依赖文件:

<?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</groupId>
   <artifactId>springboot01</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>springboot01</name>
   <description>Demo project for Spring Boot</description>

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.4.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不仅包含spring-boot-starter,还自动开启了web功能。

来看第一demo:

建个controller

@RestController
public class HelloController {

@RequestMapping("/")
public String index(){
return "Greetings from spring Boot1";
  }
}

启动SpringbootFirstApplication的main方法,打开浏览器localhost:8080,浏览器显示:

我们来看一下,默认下导入的依赖包有:

@SpringBootApplication
public class Springboot01Application {

public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
  }

@Bean
  public CommandLineRunner commandLineRunner(ApplicationContext ctx){
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] baenNames =ctx.getBeanDefinitionNames();
        Arrays.sort(baenNames);
        for (String beanName:baenNames){
System.out.println(beanName);
        }
};
  }
}

导入的包,结果如下:

接着看测试的方式:

@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((ResultMatcher) content().string(equalTo("Greetings from Spring Boot!")));
  }
}

接着来看加过,看与之前的输出有什么区别:

MockHttpServletResponse:

           Status = 200

    Error message = null

          Headers = {Content-Type=[application/json;charset=UTF-8], Content-Length=[27]}

     Content type = application/json;charset=UTF-8

             Body = Greetings from spring Boot1

    Forwarded URL = null

   Redirected URL = null

          Cookies = []

java.lang.ClassCastException: org.springframework.test.web.client.match.ContentRequestMatchers$$Lambda$351/700837405 cannot be cast to org.springframework.test.web.servlet.ResultMatcher

at com.springboot01.HelloControllerTest.getHello(HelloControllerTest.java:29)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)

at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

2018-09-11 09:34:29.112  INFO 13580 --- [       Thread-3] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@1ed6388a: startup date [Tue Sep 11 09:34:27 CST 2018]; root of context hierarchy





总结:
1.学到的东西:

    1.1.学习了Springboot的一些基础知识。

      

2.明天计划的事情:

    2.1.继续准备复盘申请。

    2.2.继续学习Spring boot和Spring cloud 。


3.遇到的问题:

 

4.收获:

       收获了上述知识点,坚持!



返回列表 返回列表
评论

    分享到