SpringBoot基础day03 - 基于springboot创建RESTful服务 - 02

SpringBoot基础day03 - 基于springboot创建RESTful服务 - 02

Tags: SpringBoot

摘要

SpringBoot基础day03 - 基于springboot创建RESTful服务 - 02

增删改查

package app.api;

import app.model.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhu on 16-12-24.
 */

@RestController
public class UserController {


    private static BigInteger nextId;
    private static Map<BigInteger, User> userMap;


    static {
        User user1 = new User();
        user1.setId(BigInteger.valueOf(1));
        user1.setAge(18);
        user1.setName("张三");

        User user2 = new User();
        user2.setId(BigInteger.valueOf(2));
        user2.setAge(28);
        user2.setName("李四");

        if (userMap == null) {
            userMap = new HashMap<BigInteger, User>();
        }
        userMap.put(user1.getId(), user1);
        userMap.put(user2.getId(), user2);
        nextId = BigInteger.valueOf(3);
    }

    //模拟保存,更新
    private static User save(User user) {
        if (userMap == null) {
            userMap = new HashMap<BigInteger, User>();
            nextId = BigInteger.ONE;
        }

        if (user.getId() != null) {
            User u = userMap.get(user.getId());
            if (u == null) {
                return null;//指定更新不存在
            }
            userMap.remove(user.getId());
            userMap.put(user.getId(), user);
            return user;
        }
        user.setId(nextId);
        nextId = nextId.add(BigInteger.ONE);
        userMap.put(user.getId(), user);
        return user;
    }


    private static boolean delete(BigInteger id) {
        User user = userMap.remove(id);
        if (user == null) {
            return false;
        }
        return true;
    }


    @RequestMapping(value = "/api/users",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity getUsers() {
        return new ResponseEntity(userMap.values(), HttpStatus.OK);
    }


    @RequestMapping(value = "/api/users/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<User> getUser(@PathVariable(value = "id") BigInteger id) {
        User user = userMap.get(id);
        if (user == null) {
            return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }


    @RequestMapping(
            value = "/api/users",
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<User> saveUser(@RequestBody User user) {
        User saveuser = save(user);
        return new ResponseEntity<User>(saveuser, HttpStatus.CREATED);
    }

    @RequestMapping(
            value = "/api/users",
            method = RequestMethod.PUT,
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public ResponseEntity<User> updateUser(@RequestBody User user) {
        User updateuser = save(user);
        if (updateuser == null) {
            return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<User>(updateuser, HttpStatus.CREATED);
    }


    @RequestMapping(value = "/api/users/{id}",
            method = RequestMethod.DELETE
    )
    public ResponseEntity<User> deleteUser(@PathVariable("id") BigInteger id) {

        boolean deleted = delete(id);
        if (!deleted) {
            return new ResponseEntity<User>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<User>(HttpStatus.NO_CONTENT);

    }


}

Rest方法

增加 POST
删除 DELETE
修改 PUT
查询 GET

注意返回结果的http响应码
查询时找不到 HttpStatus.NOT_FOUND
保存成功 HttpStatus.CREATED
更新时找不到 HttpStatus.INTERNAL_SERVER_ERROR
删除成功 HttpStatus.NO_CONTENT
查询成功 HttpStatus.OK

以前都是自己定义一些返回值状态码,感觉很不专业.
既然http协议里已经有了标准统一的状态码直接使用就好了.

代码位置

day03

https://coding.net/u/javacore/p/spring-boot-fundamentals/git/tree/master/day03