您的位置:首页 > 编程语言 > Java开发

快速搭建第一个简单的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")));
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: