1. 程式人生 > >在RESTFUL風格中怎樣定義需要多個引數才能確定的資源路徑

在RESTFUL風格中怎樣定義需要多個引數才能確定的資源路徑

@Controller
@RequestMapping("/second")
public class SController {

//如果有多個請求引數時,只需要中間用"/"分開就好,但是此時web.xml中需要配置/

@RequestMapping("/answer/{id}/{key}")

public String answer(@PathVariable("id")String id,@PathVariable("key")String key,HttpServletRequest request,HttpServletResponse response,Model model) throws Exception{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

if(key == null || !key.trim().matches("[a-z0-9]{32}")){
return "errorkey";
}
if(id==null||"".equals(id)){
return "errorURI";
}

return "OK";

}

}

返回結果:
{
"error_code": 0,
"reason": "Success",
"result": {
"data": [
{
"id": 109885,
"body": "卻上心頭。",
"valid": 0,
"title": "已有推辭之意(打一瓊瑤作品)",
"cname": "謎語"
}
]
}
}