Restful风格编码

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Restful风格编码,简单的来说用一个url来处理同一个资源的相关操作,如增删改查等。
 */

@RestController
@RequestMapping("/restful/user")
public class RestfulUserController {
    //将user存放在map中,模拟数据库
    private static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    /**
     * 返回所有用户
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public List<User> getUserList() {
        //处理restful/user 的GET请求,用来获取用户列表
        //还可以通过@RequestParam从页面中传递参数来进行条件查询或者反野信息的传递
        List<User> userList = new ArrayList<User>(users.values());
        printMap();
        return userList;
    }

    /**
     * 返回特定ID的用户
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
        // url中的id可通过@PathVariable绑定到函数的参数中
        printMap();
        return users.get(id);
    }

    /**
     * 创建新用户
     * @param user
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postUser(User user) {
        // 处理"/users/"的POST请求,用来创建User
        //@ModelAttribute User user
        // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数
        users.put(user.getId(), user);
        printMap();
        return "success";
    }

    /**
     * 使用put更新用户
     * @param id
     * @param user
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUser(@PathVariable Long id, User user) {
        //处理"/restful/user"的put请求,用来获取url中id的User信息
        User u = users.get(id);
        u.setName(user.getName());
        users.put(id, u);
        printMap();
        return "success";
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id){
        // 处理"/users/{id}"的DELETE请求,用来删除User
        // url中的id可通过@PathVariable绑定到函数的参数中
        users.remove(id);
        printMap();
        return"success";
    }
    private void printMap(){
        for (Map.Entry<Long,User> entry:users.entrySet()){
            System.out.println(entry.getValue().toString());
        }
    }
}

总结来说:

Restful风格编码,简单的来说用同一个url的不同访问方式来处理同一个资源的不同操作,如增删改查等。

简单规则:

Get->select

post->insert   

put->update  

delete->delete

例如

/user/query->/user (GET方式)

/user/add->/user (POST方式)

/user/update->/user (PUT方式)

/user/delete->/user (DELETE方式)

tip:@RequestParam与@PathVariable简单区别

@pathVariable的作用是将请求URL中的模板变量映射到功能处理方法的参数上,取出uri模板中的变量作为参数

例如访问以上的

/**
 * 返回特定ID的用户
 */
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public User getUser(@PathVariable Long id) {
    // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
    // url中的id可通过@PathVariable绑定到函数的参数中
    printMap();
    return users.get(id);
}

方法时,如果不加该注解,则使用/restful/user/2访问该方法时,方法查询不到结果。

因为没有该注解的时候实际上访问的是/restful/user/null所以查询不到结果。

@RequestParam注解的作用则是提取参数。

该注解有三个参数:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0") long parentId)

猜你喜欢

转载自my.oschina.net/mrfu/blog/1636052