博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot—03REST请求
阅读量:4562 次
发布时间:2019-06-08

本文共 3508 字,大约阅读时间需要 11 分钟。

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
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
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'}"; } }}

转载于:https://www.cnblogs.com/gispathfinder/p/8920908.html

你可能感兴趣的文章
java多态及tostring方法与equals方法的重写
查看>>
python 获取远程设备ip地址
查看>>
JDBC 第五课 —— 小项目的界面升级
查看>>
团队作业3——需求改进&系统设计
查看>>
返回json格式时间,解析时间
查看>>
线程并发-栈限制&ThreadLocal
查看>>
[转]Understand QoS at OpenSwitch
查看>>
vue中后台请求数据配置
查看>>
NIS服务器详解
查看>>
[备忘] 网络监控程序
查看>>
keepalived 高可用
查看>>
java_web学习(1)理解JavaBean
查看>>
再见,viewDidUnload方法
查看>>
233
查看>>
数据库MySQL/mariadb知识点——视图管理语句
查看>>
计数(count)
查看>>
cruise-control
查看>>
js 常用页面刷新
查看>>
HBase-TDG ClientAPI Advanced Features
查看>>
运行php程序时,浏览器跳出打开和保存提示框
查看>>