@RequestMapping(value = "/xxx/xxxxxx", method = RequestMethod.POST)
public Response fun1(HttpServletRequest request,
@PathVariable("paraA") String a,
@RequestParam("ParaB") String b,
@RequestParam("ParaC") String c ) {
//code here
return response;
}
@RequestMapping(value = "/xxx/yyyyyyy", method = RequestMethod.PUT)
public Response fun2(HttpServletRequest request,
@PathVariable("paraA") String a,
@RequestParam("ParaB") String b,
@RequestParam("ParaC") String c ) {
//code here
return response;
}
要点:
@RequestParam
A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( String--> 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值;
B)用来处理Content-Type: 为 application/x-www-form-urlencoded
编码的内容,提交方式GET、POST;
C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
*如果是json,则要以下写法:
@RequestMapping(value = "/api/xxxx/{param}", method = { RequestMethod.PUT })
public Response fun3(
@PathVariable(value = "param") String param,
HttpServletRequest request) {
logger.info("============= API start ==================");
//code service here
logger.info("============= API end ==================");
return response;
}
要用上HttpServletRequest,然后在service层,调用
JSONObject json = ParametersUtil.getRequestBodyWithJson(param,request);
ParametersUtil:
public class ParametersUtil {
private static final Logger logger = LoggerFactory
.getLogger(ParametersUtil.class);
public static JSONObject getRequestBodyWithJson(String parameter,
HttpServletRequest request) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
request.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
logger.info("Request Body:" + sb.toString());
String reqBody = URLDecoder.decode(sb.toString(), "UTF-8");
JSONObject json = new JSONObject(reqBody);
logger.info(
"[getRequestBodyWithJson][{}]-- get request body with json successfully",
parameter);
return json;
} catch (Exception e) {
logger.error(
"[getRequestBodyWithJson][{}]-- get request body with json fail, exception is [{}]",
parameter, e.getMessage());
return null;
}
}
public static JSONArray getJsonArray(JSONObject jsonObject, String key) {
try {
if (jsonObject == null) {
return null;
} else {
return jsonObject.getJSONArray(key);
}
} catch (Exception e) {
return null;
}
}
public static String covertJSONArray2String(JSONArray jsonArray) {
if (jsonArray == null || jsonArray.length() == 0) {
return null;
}
List<String> list = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
String s = (String) jsonArray.get(i);
if (s != null && !s.isEmpty()) {
list.add(s.trim());
}
}
if (list.isEmpty()) {
return null;
} else {
return list.toString().replace("[", "").replace("]", "")
.replace(" ", "");
}
}
public static String getJsonValue(JSONObject jsonObject, String key) {
try {
if (jsonObject == null) {
return null;
} else {
return jsonObject.getString(key);
}
} catch (Exception e) {
return null;
}
}
public static JSONObject getJsonObject(JSONObject jsonObject, String key) {
try {
if (jsonObject == null) {
return null;
} else {
return jsonObject.getJSONObject(key);
}
} catch (Exception e) {
return null;
}
}
}