快速搭建第一个简单的SpringBoot项目
2019-02-22 15:39
549 查看
1.新建maven项目
2.在pom.xml中添加SpringBoot依赖
[code]<!--定义了spring boot的基础依赖 以及一些默认配置内容,比如配置文件application.properties的位置等 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath></relativePath> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <!-- 全栈web开发模块,包含tomcat,Spring MVC --> <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> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version><!--$NO-MVN-MAN-VER$--> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
3.编写测试类
① Application.java是项目的启动类(注意:Application.java所在的包一定是其他类所在包的父级包),详细如下:
[code]package com.channelsoft.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
② HelloController.java详细如下:
[code]package com.channelsoft.springboot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/boot") @RestController public class HelloController { @RequestMapping("/hello") public String hello() { return "hello"; } }
4.启动项目,直接运行Application.java,就像运行普通的java文件一样
5.项目启动默认端口是8080,在浏览器总中输入,http://localhost:8080/boot/hello,结果如下:
6.@RestController的详解:
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html
7.更多关于@RestController的解析参照下面地址:
地址:https://www.geek-share.com/detail/2725288040.html
8.编写SpringBoot测试类:
[code]package com.channelsoft.springboot.test; import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.channelsoft.springboot.Application; import com.channelsoft.springboot.controller.HelloController; //引入Spring对Junit4的支持 @RunWith(SpringJUnit4ClassRunner.class) //指定SpringBoot的启动类 @SpringApplicationConfiguration(classes=Application.class) //开启web引用的配置,用于模拟ServletContext @WebAppConfiguration public class SringBootTest { private MockMvc mvc; @Before public void before() throws Exception{ mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); } @Test public void hello() throws Exception{ mvc.perform(MockMvcRequestBuilders.get("/boot/hello") .accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.content().string(Matchers.equalTo("hello"))); } }
相关文章推荐
- SpringBoot入门-02(第一个简单的SpringBoot项目搭建)
- Spring Boot-RESTfull API简单项目的快速搭建
- 【SpringBoot教程二】:RESTfull API简单项目的快速搭建
- 003,spring boot RESTfull API简单项目的快速搭建
- springboot (1) 快速搭建简单项目
- 快速搭建一个简单的springboot项目
- Spring Boot -- RESTFul API 简单项目的快速搭建
- spring-boot(2) - RESTfull API简单项目的快速搭建
- springboot入门,简单能跑的项目快速搭建
- Spring boot 搭建一个简单的 Java EE 项目
- Spring Boot实战(四)Spring MVC基础 4.2 Spring MVC项目快速搭建
- IDEA 快速搭建springboot项目,返回HTML首页
- 快速搭建SpringBoot项目
- SpringBoot(一):快速搭建一个springboot项目
- 【SpringBoot探索一】SpringBoot快速搭建基础web项目
- 004-Spring boot 快速入门-项目搭建与启动、SpringBootApplication
- start.spring.io快速搭建springboot项目页面解析介绍
- Spring-Boot快速搭建web项目详细总结
- 小Spring Boot项目的快速搭建与开发
- 使用Springboot快速搭建项目