Springboot项目搭建
1,创建一个空的maven项目
2,pom文件添加父项目和web起步依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<!-- web start -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- web end -->
3,建立入口类
package cc.wangshuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 来标注这是一个主程序
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}
4,添加测试Controller
package cc.wangshuang.controller;
import cc.wangshuang.dto.BaseResultDto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public BaseResultDto hello() {
BaseResultDto resultDto = new BaseResultDto();
resultDto.success();
return resultDto;
}
}
package cc.wangshuang.dto;
import cc.wangshuang.dictionary.RESULT;
/**
* @Author: cool
* @Date: 2019/1/10 18:23
*/
public class BaseResultDto {
private String result_code;
private String result_message;
public String getResult_code() {
return result_code;
}
public void setResult_code(String result_code) {
this.result_code = result_code;
}
public String getResult_message() {
return result_message;
}
public void setResult_message(String result_message) {
this.result_message = result_message;
}
public BaseResultDto(String result_code, String result_message) {
super();
this.result_code = result_code;
this.result_message = result_message;
}
public BaseResultDto() {
}
@Override
public String toString() {
return "BaseResultDto [result_code=" + result_code + ", result_message=" + result_message + "]";
}
public void success() {
this.result_code = RESULT.Success.getCode();
this.result_message = RESULT.Success.getName();
}
public void fail(String result_message) {
this.result_code = RESULT.Failure.getCode();
this.result_message = result_message;
}
public void fail() {
fail(RESULT.Failure.getName());
}
}
package cc.wangshuang.dictionary;
/**
* @Author: cool
* @Date: 2019/1/10 18:26
*/
public enum RESULT {
Success("0", "成功","succ"), Failure("1", "失败","fail"),EXCEPTION("-1", "异常", "exception");
private String code;
private String name;
private String gv_code;
private RESULT(String code, String name, String gv_code) {
this.code = code;
this.name = name;
this.gv_code = gv_code;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public String getGv_code() {
return gv_code;
}
}
5,添加单元测试
package cc.wangshuang;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* @Author: cool
* @Date: 2019/1/14 10:47
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = HelloWorldMainApplication.class)
@WebAppConfiguration
public class BaseTest {
@Test
public void contextLoads() {
//测试加载项目
}
}
运行测试方法contextLoads不报错就成功了
6,启动web项目
运行 HelloWorldMainApplication 入口类的main方法,web项目就启动啦
启动后,访问 http://127.0.0.1:8888/hello
正常返回
{
result_code: "0",
result_message: "成功"
}