天天看点

restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例

本文主要记录搭建RESTful API标准工程,包含比较推荐的工程结构,掌握一些基本注解,并引入Swagger

新建一个项目

通过Spring Initializr创建工程,选择所需要的jar包,如下图:

restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例
restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例
restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例

工程结构

Spring Boot框架对工程结构并没有什么特殊的限制,我这边基本按照网上主流和自己一些喜好进行划分,供参考:

代码层结构

前端控制器(Controller): com.xxx.controller

数据服务层(Service): com.xxx.service

实体(Entity)与数据访问层(Repository):com.xxx.domain

公共方法及工具类: com.xxx.common

资源文件的结构

配置文件: src/main/resources/config

静态文件: src/main/resources/static

模板: src/main/resources/templates

restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例

编写第一个服务

在controller下新建类HelloController,相关代码做了简单的注释,如下:

//相当于 @Controller + @ResponseBody

//该注解 方法method 返回类型是String时候则返回string,返回对象时候则讲json_encode 该对象的json字符串

@RestController

public class HelloController {

//该注解mapping指定路由

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public String SayHello() {

return "Hello Spring Boot";

}

}

编写到这里,已经可以直接编译运行了,这里值得注意的是被@SpringBootApplication注解的启动类一定要放在所有的RestController的根路径的package下,@SpringBootApplication只会扫描@SpringBootApplication注解标记类包下及其子包的类,如果不放在根路径下,可以指定下:@SpringBootApplication(scanBasePackages = "com.example.api_demo")

无需配置Tomcat,直接启动,输入对应的地址可看到结果:

restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例
restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例

增加service层

在搭建了基础应用的基础上,我们增加service层抽离控制层和业务层代码。

在service下新增HelloService和HelloServiceImpl两个类,代码如下:

//业务层接口:HelloService

public interface HelloService {

public String sayHello();

}

//接口实现:HelloServiceImpl

@Service

public class HelloServiceImpl implements HelloService{

@Override

public String sayHello()

{

return "Hello Spring Boot";

}

}

修改对应的controller代码:

@Autowired

private HelloService helloService;

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public String sayHello()

{

return helloService.sayHello();

}

这样,简单的拆分就完成了。

引入Swagger

Swagger是什么大家自行百度,对于Restful API来说,Swagger绝对是它的好基友。

首先引入对应的jar包,在pom.xml加入:

io.springfox

springfox-swagger2

2.7.0

io.springfox

springfox-swagger-ui

2.7.0

新增类Swagger2.java,代码如下:

@Configuration

@EnableSwagger2

public class Swagger2 {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.basePackage("com.example.api_demo.controller"))

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("测试文档")

.description("这里是一段描述")

.termsOfServiceUrl("http://www.bug2048.com/")

.version("1.0")

.build();

}

}

到这里,Swagger就算配置完成了,接下来就是要添加文档内容了,修改之前的HelloController之后,直接编译运行,通过/swagger-ui.html就能看到效果

@ApiOperation(value="增加Service层输出Hello", notes="这是第二个demo")

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public String sayHello()

{

return helloService.sayHello();

}

restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例
restful api 例子 java_Spring Boot学习笔记(四)构建RESTful API标准工程实例

至此,简单的框架算是完成了,后面就可以逐步完善,包括接入日志,数据库等等,后面会持续更新。

总结

基于Spring Boot构建RESTful API相对来说还是比较便捷的,其中注解使得代码更加简洁,本次用到注解再汇总下,有时间的话可以深入理解下其背后的原理:

@SpringBootApplication: 申明让spring boot自动给程序进行必要的配置。

@RestController:REST风格的控制器

@RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射

@Service:一般用于修饰service层的组件

@Autowired: 自动导入依赖的bean