为什么要写这篇贴?

  要写一个最简单的CRUD 符合 Restful Api    规范的  一个Controller, 想百度搜索一下 直接复制拷贝 简单修改一下 方法内代码。

  然而, 搜索结果让我无语到家。 没一个是正在符合 Restful Api 规范的实例。 最无语的是 你呀直接 JSP 页面了,还说什么  Restful Api 啊!!!

  为方便以后自己复制拷贝使用,我把自己刚写的贴出来。

  

Swagger2:
@Configuration
@EnableSwagger2
public class Swagger2
{
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.dj.edi.web"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("EID 用户 CRUD")
.description("EID 用户 CRUD")
.version("1.0")
.build();
} }
Application:
@SpringBootApplication
@Import(springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration.class)
public class ComEdiOrderUserApplication
{
public static void main(String[] args) {SpringApplication.run(ComEdiOrderUserApplication.class, args);} }
UserApiController:

@RestController
@RequestMapping("/v1/user")
public class UserApiController
{
private static final Logger LOGGER = LoggerFactory.getLogger(UserApiController.class); @Autowired
private ClientUsersRepository repository; @ApiOperation(value = "获取所有用户数据")
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ResponseEntity<List<ClientUsers>> getClientUsersList() {
try {
return ResponseEntity.ok(repository.findAll());
} catch (Exception e) {
LOGGER.info(" 获取所有用户数据异常 " + e.getMessage(), e);
return ResponseEntity.status(500).body(null);
}
} @ApiOperation(value = "获取用户数据")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<ClientUsers> getClientUsers(@PathVariable String id) {
try {
return ResponseEntity.ok(repository.findOne(id));
} catch (Exception e) {
LOGGER.info(" 获取用户数据 " + id + " 数据异常 " + e.getMessage(), e);
return ResponseEntity.status(500).body(null);
}
} @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
@ApiImplicitParam(name = "users", value = "用户详细实体user", required = true, dataType = "ClientUsers", paramType = "body")
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<ClientUsers> createUser(@Valid @RequestBody ClientUsers users) {
try { users.setId(ObjectId.get().toString());
return ResponseEntity.ok(repository.save(users)); } catch (Exception e) {
LOGGER.info(" 创建用户 " + users + " 数据异常 " + e.getMessage(), e);
return ResponseEntity.status(500).body(null);
}
} @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "ClientUsers", paramType = "body")
})
@RequestMapping(value = "{id}", method = RequestMethod.PUT)
public ResponseEntity<ClientUsers> updateUser(@PathVariable("id") String id,@Valid @RequestBody ClientUsers user) {
try {
user.setId(id);
return ResponseEntity.ok(repository.save(user));
} catch (Exception e) {
LOGGER.info(" 更新用户 " + user + " 数据异常 " + e.getMessage(), e);
return ResponseEntity.status(500).body(null);
}
} @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String", paramType = "path")
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteUser(@PathVariable String id) {
try {
repository.delete(id);
return ResponseEntity.ok("ok");
} catch (Exception e) {
LOGGER.info(" 删除用户 " + id + " 数据异常 " + e.getMessage(), e);
return ResponseEntity.status(500).body(null);
}
}
}

ClientUsersRepository:
@Component
public interface ClientUsersRepository extends MongoRepository<ClientUsers, String>
{
ClientUsers findByips(String ip);
ClientUsers findByclientFlag(String clientFlag);
}
ClientUsers:
@Data
public class ClientUsers implements Serializable
{ @Id
private String id; /**
* 用户名称
*/
@NotBlank(message = "用户名称 不能为空")
@Pattern(regexp = "^(?!string)",message = "不能是 stirng")
private String userName; /**
* ip
*/
@NotNull(message = "ip 至少需要个")
private List<String> ips; /**
* 标识
*/
@NotBlank(message = " 标识 不能为空")
@Pattern(regexp = "^(?!string)",message = "不能是 stirng")
private String clientFlag; /**
* 客户服务ID
*/
@NotBlank(message = "客户服务ID 不能为空")
@Pattern(regexp = "^(?!string)",message = "不能是 stirng")
private String checkID;
}
有哪里不好的希望指正
 

Restful Api CRUD 标准示例 (Swagger2+validator)的更多相关文章

  1. 虚拟研讨会:如何设计好的RESTful API?

    http://www.infoq.com/cn/articles/how-to-design-a-good-restful-api/ REST架构风格最初由Roy T. Fielding(HTTP/1 ...

  2. 虚拟研讨会:如何设计好的RESTful API(转)

    原文:虚拟研讨会:如何设计好的RESTful API? REST架构风格最初由Roy T. Fielding(HTTP/1.1协议专家组负责人)在其2000年的博士学位论文中提出.HTTP就是该架构风 ...

  3. 如何设计好的RESTful API 之好的RESTful API 特征

    原文地址:http://blog.csdn.net/ywk253100/article/details/25654021 导读:设计好RESTful API对于软件架构的可扩展性.可伸缩性和消费者的体 ...

  4. 最好用的koa2+mysql的RESTful API脚手架,mvc架构,支持node调试,pm2部署。

     #基于webpack构建的 Koa2 restful API 服务器脚手架    这是一个基于 Koa2 的轻量级 RESTful API Server 脚手架,支持 ES6, 支持使用TypeSc ...

  5. 我所理解的RESTful Web API [Web标准篇]

    REST不是一个标准,而是一种软件应用架构风格.基于SOAP的Web服务采用RPC架构,如果说RPC是一种面向操作的架构风格,而REST则是一种面向资源的架构风格.REST是目前业界更为推崇的构建新一 ...

  6. SpringBoot使用Swagger2实现Restful API

    很多时候,我们需要创建一个接口项目用来数据调转,其中不包含任何业务逻辑,比如我们公司.这时我们就需要实现一个具有Restful API的接口项目. 本文介绍springboot使用swagger2实现 ...

  7. 整合swagger2生成Restful Api接口文档

    整合swagger2生成Restful Api接口文档 swagger Restful文档生成工具 2017-9-30 官方地址:https://swagger.io/docs/specificati ...

  8. Spring Boot中使用Swagger2生成RESTful API文档(转)

    效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox ...

  9. Swagger2在DBA Service中生成RESTful API的实践

    目的与背景: 目的:对外暴露DBA Service必要的RESTful API,形成规整的API文档 背景:DBA Service后端采用Spring-boot,属于Spring家族,故生成API的工 ...

随机推荐

  1. Poloniex API 文档

    Examples PHP wrapper by compcentral: http://pastebin.com/iuezwGRZ Python wrapper by oipminer: http:/ ...

  2. 将vi or vim中的内容复制到terminal中

    1. 查看 vim 是否支持 clipboard 功能 $ vim --version | grep clipboard 2. 如果有 +clipboard 则跳过这一步; 如果显示的是 -clipb ...

  3. 【Unity Shader编程】之十六 基于MatCap实现适于移动平台的“次时代”车漆Shader

    本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接:http://blog.csdn.net/poem_qianmo/article/details/55803629 渲染本文配图使用的 ...

  4. LayoutInflater的动态增加控件

    在实际开发中LayoutInflater这个类是非常有用的,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件. 而findView ...

  5. 【开发者笔记】归并排序过程呈现之java内置GUI表示

    在网上看到一个视频将各种排序用视频表示出来,配上音乐,挺好玩的样子,就算是不会编程的人看到也会觉得很舒服,碰巧我也正在写归并算法,于是就用java的GUI实现一个. 归并排序的时间复杂度是T(n)=O ...

  6. XSS注入学习

    引贴: http://mp.weixin.qq.com/s?__biz=MzIyMDEzMTA2MQ==&mid=2651148212&idx=1&sn=cd4dfda0b92 ...

  7. C++学习笔记-隐式成员函数

    通过一个例子来复习C++操作符重载及隐式成员函数.MyString类模仿标准string类,简单的实现了构造一个字符串.字符串比较.取单个字符等功能.如下: #ifndef MYSTRING_H_ # ...

  8. 关于/proc/进程idpid/fd ,根据fd来查找连接

    当创建好epoll句柄后,它就是会占用一个fd值,在linux下如果查看/proc/进程id/fd/,是能够看到这个fd的,所以在使用完epoll后,必须调用close()关闭,否则可能导致fd被耗尽 ...

  9. Selenium2.0 Webdriver 随笔

    Webdriver can't action the element when the element is out of view 1. Scroll to the element use Java ...

  10. docker——安装

    Docker划分为CE和EE.CE即社区版(免费,支持后期三个月),EE即企业版,强调安全,付费使用. #安装依赖包 yum install -y yum-utils device-mapper-pe ...