一、springboot框架中,项目配置文件
spring:application:
name: springboot
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@ip:1521:orcl
username: i
password: p
server:
port: 8001
context-path: /springboot
mybatis:
mapperLocations: classpath*:mapper/*.xml
redis:
host: 127.0.0.1
port: 6379
password:
timeout: 10000
pool:
max-idle: 20
min-idle: 5
max-active: 20
max-wait: 2
采用yml格式编写项目配置,数据库、mybatis、redis等都采用配置进行。
二、controller 层代码解析
@RestController
@RequestMapping(value="/users") // 通过这里配置使下面的映射都在/users下,可去除
public class UserController {
@Autowired
private WarnUserServiceImpl warnUserServiceImpl;
@ApiOperation(value="获取提醒人员列表", notes="")
@RequestMapping(value={""}, method=RequestMethod.GET)
public Map getUserList() {
List<WarnUser> list = warnUserServiceImpl.infoList();
Map map = new HashMap();
map.put("userList",list);
return map ;
}
@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@RequestMapping(value="/a", method=RequestMethod.POST)
public Map<String, User> postUser(@RequestBody User user) {
return null;
}
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/1", method=RequestMethod.GET)
public Map<String, User> getUser(@PathVariable Long id) {
return null;
}
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})
@RequestMapping(value="/2", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody User user) {
return "success";
}
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/3", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
return "success";
}
}
@@RestController
:Spring4之后加入的注解,原来在@Controller
中返回json需要@ResponseBody
来配合,如果直接用@RestController
替代@Controller
就不需要再配置@ResponseBody
,默认返回json格式。
method - 指定请求的方法类型:POST/GET/DELETE/PUT 等value - 指定实际的请求地址consumes - 指定处理请求的提交内容类型,例如 Content-Type 头部设置application/json, text/html
produces - 指定返回的内容类型