为什么要写这篇贴?

  要写一个最简单的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. Java实现断点续传

    原理: 断点续传的关键是断点,所以在制定传输协议的时候要设计好,如上图,我自定义了一个交互协议,每次下载请求都会带上下载的起始点,这样就可以支持从断点下载了,其实HTTP里的断点续传也是这个原理,在H ...

  2. 剑指Offer——按之字形顺序打印二叉树

    题目描述: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 分析: 我们都知道二叉树的层次遍历用的是队 ...

  3. GITLAB服务基础

    1.GITLAB介绍 一个基于GIT的源码托管解决方案基于Ruby on rails开发集成了nginx postgreSQL redis sidekiq等组件 2. 资源 官网:https://ab ...

  4. Power Strings----poj2406(kmp扩展 循环节)

    题目链接:http://poj.org/problem?id=2406 题意:就是求串s能够最多由多少个相同的串a串联而成: 例如 ababab 由3个ab串联而成: abababa 只能由1个aba ...

  5. 如何缩减手游app安装包的大小?

    包体过大对手游的影响更是诟病已久,有具体数据证明,游戏包体越大,在游戏运营推广过程中游戏用户的转化率就越低:反之,游戏包体越小,游戏用户的下载转化率就越高(如下图),所有的手机app.游戏在大版本更新 ...

  6. Python并行编程(十):多线程性能评估

    1.基本概念 GIL是CPython解释器引入的锁,GIL在解释器层面阻止了真正的并行运行.解释器在执行任何线程之前,必须等待当前正在运行的线程释放GIL,事实上,解释器会强迫想要运行的线程必须拿到G ...

  7. Python之配置文件读写

    ConfigParser模块 一.创建配置文件 在D盘建立一个配置文件,名字为:test.ini 内容如下: [baseconf] host=127.0.0.1 port=3306 user=root ...

  8. Linux ssh服务

    关于ssh服务不多说就提几句,1,机房的服务器一般都是通过远程连接登录的,远程登录就必然少不了ssh客户端.2,虚拟机每次都要点击进去,每次退出来也需要按Ctrl+Alt+Enter,也比较麻烦,有时 ...

  9. Html5游戏开发-145行代码完成一个RPG小Demo

    lufy前辈写过<[代码艺术]17行代码的贪吃蛇小游戏>一文,忽悠了不少求知的兄弟进去阅读,阅读量当然是相当的大.今天我不仿也搞一个这样的教程,目地不在于忽悠人,而在于帮助他人. 先看de ...

  10. facebook工具xhprof的安装与使用-分析php执行性能(转载)

    下载源码包的网址 http://pecl.php.net/package/xhprof