快速搭建第一个简单的SpringBoot项目

1.新建maven项目

2.在pom.xml中添加SpringBoot依赖

<!--定义了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所在的包一定是其他类所在包的父级包),详细如下:

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详细如下:

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.cnblogs.com/shuaifing/p/8119664.html

8.编写SpringBoot测试类:

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")));
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星***满天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值