Restful服务

Restful Web API应该严格遵守Http的四个操作

            1、GET对应查询操作;

            2、POST对应创建操作;

            3、PUT对应更新操作;

            4、DELETE对应删除操作。

从HttpClient发过来的Request类型只能是上述四种之一。

下面是我做的一个例子,这样可以更快的理解

import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cf.pojo.User;
import com.cf.rest.service.RestService;

@Controller
public class RestController {

	@Autowired
	private RestService restService;
	
	/**
	 * 查询操作 REST风格 : /user/用户id
	 * 
	 */
	@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String get(@PathVariable("id") Long id) {
		User user = restService.selectByPrimaryKey(id);
		String result = JSON.toJSONString(user);
		System.out.println("---------------------------get:" + result);
		
		return result;
	}
	
	/**
	 * 创建操作 REST风格
	 * 
	 */
	@RequestMapping(value = "/user/register", method = RequestMethod.POST)
	@ResponseBody
	public String register(User user) {

		String result = JSON.toJSONString(user);
		System.out.println("---------------------------Post:" + result);
		
		return result;
	}
	
	/**
	 * 删除操作 REST风格
	 * 
	 */
	@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public String delete(@PathVariable("id") Long id) {

		System.out.println("---------------------------delete:" + id);
		
		return "delete";
	}
	
	/**
	 * 更新操作 REST风格
	 * 
	 */
	@RequestMapping(value = "/user/update", method = RequestMethod.PUT)
	@ResponseBody
	public String update(User user) {

		String result = JSON.toJSONString(user);
		System.out.println("---------------------------Put:" + result);
		
		return result;
	}
	
	/**
	 * 传统风格 : /user?name户zhangsan&age=18
	 * 
	 */
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	@ResponseBody
	public String search(@RequestParam("name") String queryString) {
		
		System.out.println("---------------------------get:" + queryString);
		
		return queryString;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40106067/article/details/83098142