Spring Boot构建RESTful

目录

 

RESTful设计

RESTful完美实践

 


RESTful设计

RESTful完美实践

package com.example.demo.controller;

import com.example.demo.entity.Person;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value="/person")
public class PersonController {

    //create map
    static Map<Integer, Person> persons = Collections.synchronizedMap(new HashMap<Integer, Person>());

    @RequestMapping(value="/", method= RequestMethod.GET)
    public List<Person> getPersonList() {
        List<Person> p = new ArrayList<>(persons.values());
        return p;
    }
    @RequestMapping(value="/{id}", method= RequestMethod.GET)
    public Person getPerson(@PathVariable int id) {
        return persons.get(id);
    }

    @RequestMapping(value="/{id}", method= RequestMethod.PUT)
    public String updateUser(@PathVariable int id, @ModelAttribute Person person) {
        Person p = persons.get(id);
        p.setPassword(person.getPassword());
        p.setUserName(person.getUserName());
        persons.put(id,p);
        return "ok";
    }
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deletePerson(@PathVariable int id) {
        persons.remove(id);
        return "ok";
    }
}

 

猜你喜欢

转载自blog.csdn.net/qq_34625785/article/details/92805208