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

}
原文地址:https://www.cnblogs.com/gispathfinder/p/8920908.html