Spring Boot—03REST请求
package com.smartmap.sample.ch1.controller.rest; import java.util.List; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.smartmap.sample.ch1.entity.User;
import com.smartmap.sample.ch1.service.UserService; @RestController
@RequestMapping("/api/v1.1/system/user")
public class UserRestController {
private final Log logger = LogFactory.getLog(UserRestController.class); @Autowired
UserService userService; /**
* 查询所有用户
*
* curl -XGET 'http://127.0.0.1:8080/api/v1.1/system/user/'
*
* @return
*/
@GetMapping("/")
public List<User> getAllUsers() {
return userService.allUser();
} /**
* 根据Id查询用户
*
* curl -XGET 'http://127.0.0.1:8080/api/v1.1/system/user/123'
*
* @param userId
* @return
*/
@GetMapping("/{userId}")
public User getUserById(@PathVariable("userId") Long userId) {
return userService.getUserById(userId);
} /**
* 翻页查询用户
*
* curl -XGET
* 'http://127.0.0.1:8080/api/v1.1/system/user/query?offset=123&limit=456&sortBy=789&sortOrder=456'
*
* @param offset
* @param limit
* @param sortBy
* @param sortOrder
* @return
*/
@GetMapping("/query")
public List<User> queryUserById(@RequestParam("offset") int offset, @RequestParam("limit") int limit,
@RequestParam("sortBy") int sortBy, @RequestParam("sortOrder") int sortOrder) {
logger.info(String.valueOf(offset));
logger.info(String.valueOf(limit));
logger.info(String.valueOf(sortBy));
logger.info(String.valueOf(sortOrder));
return userService.allUser();
} /**
* 添加用户
*
* curl -XPOST 'http://127.0.0.1:8080/api/v1.1/system/user/'
* -H'Content-type:application/json;charset=UTF-8' -d ' { "id": "123",
* "name":"123" } '
*
* @param user
* @return
*/
@PostMapping("/")
public User addUse(@RequestBody User user) {
System.out.println(user.getName());
return userService.save(user);
} /**
* 更新用户
*
* curl -XPUT 'http://127.0.0.1:8080/api/v1.1/system/user/'
* -H'Content-type:application/json;charset=UTF-8' -d ' { "id": "123",
* "name":"123" } '
*
* @param user
* @return
*/
@PutMapping("/")
public User updateUse(@RequestBody User user) {
return userService.save(user);
} /**
* 删除用户
*
* curl -XDELETE 'http://127.0.0.1:8080/api/v1.1/system/user/123'
*
* @param userId
* @return
*/
@DeleteMapping("/{userId}")
public String deleteUser(@PathVariable("userId") Long userId) {
if (userService.delete(userId) > 0) {
return "{success:true, message:'delete success'}";
} else {
return "{success:false, message:'delete fail'}";
}
} }
Spring Boot—03REST请求的更多相关文章
- spring boot 并发请求,其他系统接口,丢失request的header信息【多线程、线程池、@Async 】
场景:一次迭代在灰度环境发版时,测试反馈说我开发的那个功能,查询接口有部分字段数据是空的,后续排查日志,发现日志如下: feign.RetryableException: cannot retry d ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
- Spring Boot 对请求的映射
在SpringBoot中对请求资源的映射有三种方式: 1.通过编写一个Controller请求,获得客户端发送过来的请求就转发出去 //通过这种方式可以来映射页面请求路径 @PostMapping(& ...
- Spring Boot使用AOP在控制台打印请求、响应信息
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等. AOP简介 AOP全称Aspect Oriented Programming,面向切面,AOP主要实现的 ...
- Spring Boot实现一个监听用户请求的拦截器
项目中需要监听用户具体的请求操作,便通过一个拦截器来监听,并继续相应的日志记录 项目构建与Spring Boot,Spring Boot实现一个拦截器很容易. Spring Boot的核心启动类继承W ...
- Spring Boot 系列(二)单元测试&网络请求
实际开发中,Junit单元测试是必不可少的.在spring-boot 中可以通过测试模块(spring-boot-starter-test)快速使用单元测试功能. 开始 本示例在 spring boo ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- Spring Boot 2.0 教程 | AOP 切面统一打印请求日志
欢迎关注微信公众号: 小哈学Java 文章首发于个人网站 https://www.exception.site/springboot/spring-boot-aop-web-request 本节中,您 ...
- spring boot 请求地址带有.json 兼容处理
项目以前时spring mvc的,现在升级为spring boot ,有些请求地址带有.json后缀,在请求spring boot项目时,无法匹配控制器,spring boot默认选择禁用后缀模式匹配 ...
随机推荐
- 使用git时出现Please make sure you have the correct access rights and the repository exists.问题已解决。
使用git时,出现Please make sure you have the correct access rights and the repository exists.问题已解决. 今天我在使用 ...
- python kmeans实战 - 单机一层聚类(小玩具哦),下次再弄个分布式多次聚类
# coding=utf-8 """ #K-means """ import time ...
- (转)PLSQL Developer 12.0.7连接Oracle12c数据库
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sl1992/article/details/80489413 1.下载安装PL/SQL Develo ...
- EJB3 阶段总结+一个EJB3案例 (1)
经过一段时时间的学习,对EJB3的相关知识和jboss8的配置有了大概的了解. 网上对EJB的评论很多,基本都是负面的,都表示EJB太过于沉重,不容易维护.但通过这段时间的学习,私下认为,EJB3在某 ...
- TCP与UDP的差别以及TCP三次握手、四次挥手
UDP: 1.UDP面向报文,无需建立连接,不可靠,数量小,高层就解决差错重传,无需拥塞控制 2.支持音频.视频传输 3.检查和检验UDP包头和数据和伪首部 4.分组开销小(头部8个字节),提供最大努 ...
- CS231n学习笔记-图像分类笔记(上篇)
原文地址:智能单元 图像分类:所谓图像分类问题,就是已有固定的分类标签集合,然后对于输入的图像按照标签类别,将其打上标签. 下面先介绍一下一个简单的图像如何利用计算机进行分类: 例子:以下图为例,图像 ...
- .gitignore释疑
自己总结: *.a--匹配任意路径下的.a文件 a/--匹配任意路径下的a文件夹 a/*--匹配根目录下的文件夹a下的任意文件(等同于 /a/* 或 /a/) ----一般情况下不会有这种写法 /a ...
- getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- 工具类APP
应用名称 工具S 英文名称 未填写 应用描述 工具类APP 英文描述 未填写 应用官网 this 应用图标
- idea 错误: -source 1.6 中不支持 diamond 运算符的解决办法
在取一段github代码时,发现说是支持jdk 7 ,但是使用MAVEN编译不过去. 报错信息为错误: -source 1.6 中不支持 diamond 运算符 我使用的环境是1.7 + intel ...