Spring Restful APIを構築し、パラメータ解析を要求する。


Spring restful appインターフェースを作成するのは非常に一般的な手段でhttpリクエストサービスを受けます。パラメータの受け取り方とMockを使ったテストは探究に値すると思います。
Spring Restfule APIリクエストタイプ
1要求パラメータなし
パラメータが要求されていません。要求経路を指定して、対応する要求を受信します。簡単な例を挙げます。
@RequestMapping("/c/manage/qcs/client")
@Controller
public class QcsClientManageController  {

    @RequestMapping(value="/list",method=RequestMethod.GET)
    public ModelAndView listConfigure() {
        ModelAndView mv = new ModelAndView();
        ...
        return mv;
    }
}
要求例:{base}/c/manager/qcs/client/list
@Request Mapping:要求された経路と方法を設定しました。これは要求アドレスマッピングを処理する注釈であり、クラスまたは方法で利用できる。クラス上の場合、クラス内のあらゆる方法に対応する要求は、そのマッピングを親アドレスとする。
@Controller:コントローラ、つまりSpring MVCの中の“C”、つまりControllerです。value:方法に対応する要求経路
method:対応する方法を要求します。GET、PUT、POST、DELETE、HEADがあります。
GET:URI対応情報を取得する
POST:該当値を作成する
PUT:作成/更新
DELETE:削除
HEAD:ファイルが存在するかどうかを取得します。POSTPUTの違いを説明します。  POST 常に新しいものを作成します。PUT 作成を表すことができますが、指定されたURIが存在すると更新という意味です。言い換えれば、PUTの要求は繰り返し実行され、結果は同じであるべきである。
2要求経路にパラメータがあります。
方法1:
@RequestMapping("/r/qcs")
@Controller
public class QcsController extends BasicController {
	@RequestMapping(value="/whitelistlogically",method = RequestMethod.GET)
	@ResponseBody
	public List<String> getWhitelistLogically(
		@RequestParam(value="entry",required = true) String entry,
		@RequestParam(value="volume",required = true) String volume) {
		return qcsService.getWhitelistLogically(entry,volume);
	}
}
要求パス:
{base}/r/qcs/whitelistlogcaly?entry={a}&volume={b}
@ResponseBody:コンテンツまたはオブジェクトをHTTP応答本文として返し、@ResponseBodyを使ってビュー処理部分を表にし、Http Message Coverterに適合するよう呼び出し、リターン値を出力ストリームに書き込みます。
@Request Param:パラメータの注釈を要求し、代表方法パラメータはウェブ要求パラメータと紐付けされるべきである。
required:パラメータが必要かどうか、trueなら必須です。falseを必要としない。
方法二:
@RequestMapping("/r/qcs")
@Controller
public class QcsController extends BasicController {
	@RequestMapping(value="/whitelistlogically",method = RequestMethod.GET)
	@ResponseBody
	public List<String> getWhitelistLogically(HttpServletRequest request) {
		String entry = RequestUtils.getParam(request, "name", null);
		String volume = RequestUtils.getParam(request, "volume", null);
		return qcsService.getWhitelistLogically(entry,volume);
	}
	
	/**
     *              
     * 
     * @param req
     *            HttpServletRequest
     * @param param
     *               
     * @param defaultValue
     *               
     * @return String
     */
    public static String getParam(HttpServletRequest req, String param, String defaultValue) {
        String value = req.getParameter(param);
        return (StringUtils.isEmpty(value)) ? defaultValue : value.trim();
    }
}
要求パス:
{base}/r/qcs/whitelistlogcaly?entry={a}&volume={b}
要求パラメータはHttpServletRequestを解析して取得する。
3 Jsonパラメータは、オブジェクトのインスタンスによって受け入れられます。
一般的にPOST要求に対して、Jsonを通して入る必要があるパラメータが多い場合のデータです。
@RequestMapping("/r/qcs")
@Controller
public class QcsController extends BasicController {
    @RequestMapping(value = "/v1/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> addClientToWhiteList(@RequestBody QcsClientEntity qcsClientRequest,
    								 HttpServletResponse response) throws Exception{	
    	String location = qcsClientRequest.getLocation();
    	String production = qcsClientRequest.getProduction();
    	String type = qcsClientRequest.getType();
    	String rolename = qcsClientRequest.getRolename();
    	String[] clients = qcsClientRequest.getClientips();
    	String callbackURI = qcsClientRequest.getCallbackuri();
    	String token = qcsClientRequest.getToken();
		...
	}
}

public class QcsClientEntity {
	String location;
	String production;
	String type;
	String rolename;
	String[] clientips;
	String callbackuri;
	String token;
	
	public String getToken() { return token; }
	public void setToken(String token) { this.token = token; }
	public String getLocation() { return location; }
	public void setLocation(String location) { this.location = location; }
	public String getProduction() { return production; }
	public void setProduction(String production) { this.production = production; }
	public String getType() { return type; }
	public void setType(String type) { this.type = type; }
	public String getRolename() { return rolename; }
	public void setRolename(String rolename) { this.rolename = rolename; }
	public String[] getClientips() { return clientips; }
	public void setClientips(String[] clientips) { this.clientips = clientips; }
	public String getCallbackuri() { return callbackuri; }
	public void setCallbackuri(String callbackuri) { this.callbackuri = callbackuri;}

}
は、オブジェクトエンティティを介して要求を受け付け、要求オブジェクトを解析し、対応するパラメータを得る。解析すればいいです
4パスのパラメータ
@RequestMapping("/r/qcs")
@Controller
public class QcsController extends BasicController {
	@RequestMapping(value="/whitelistlogically/{entry}/{volume}",method = RequestMethod.GET)
	@ResponseBody
	public List<String> getWhitelistLogically(
		@PathVariable String entry,
		@PathVariable String volume) {
		return qcsService.getWhitelistLogically(entry,volume);
	}
}
対応するパス:{base}/r/qcs/white elistlogcaly?entry={entry}&volume={volume}
@PathVarable:バインディング要求が送信された値は、方法のパラメータに適用されます。
@Request Mapping(value="/whitelist logcaly/{entry}/{volume}と、method=Request Method.GET)と @PathVarable String entry、@PathVarable String volume一対一で、名前によってマッチングして、retsfulスタイルです。マップ名が一致しない場合は、別の方法を使います。
@RequestMapping("/r/qcs")
@Controller
public class QcsController extends BasicController {
	@RequestMapping(value="/whitelistlogically/{entry}/{volume}",method = RequestMethod.GET)
	@ResponseBody
	public List<String> getWhitelistLogically( @PathVariable("entry") String paramEntry,
						   @PathVariable("volume") String paramVolume) {
		return qcsService.getWhitelistLogically(entry,volume);
	}
}
以上は主にいくつかのspring restful appiがパラメータを取得する方法であり、後続のレビューを準備するためである。
参考資料:
>http://liuyanwei.jumppo.com/2015/05/28/spring-2.html
>http://stackoverflow.com/questions/11291933/requestbody-and-responsebody-spring
Author:記憶の独秀
メール:[email protected]
出典を明記する:http://blog.csdn.net/lavorange/article/details/50696936