HTTP Content-Type:
GET请求URL传递参数(Content-Type可能为空):
功能描述:URL地址传递参数,通用:@RequestParam、@PathVariable和对象接收,局部类型转换(日期类型)。
package com.w; import com.alibaba.; import com.w.param.UrlEncodedTransfer; import org.; import org.; import org.; import org..*; import org.; import javax.; import javax.; import java.; import java.; import java.u; import java.u; import java.u; @Controller("/urlParamTransfer") @RequestMapping("/urlParamTransfer") public class UrlParamTransferController { @RequestMapping(path = "/bean1") @ResponseBody public Object bean1(HttpServletRequest request, HttpServletResponse response) { Map<String, String[]> paramsMap = reque(); if (paramsMap != null) { for (String key : ()) { Sy(key + "=" + Arrays.toString(key))); } } return JSON.toJSONString(paramsMap); } @RequestMapping(path = "/bean2") @ResponseBody public Object bean2(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("name") String[] names, @RequestParam("date") String dateStr) { UrlEncodedTransfer urlParamTransfer = new UrlEncodedTransfer(); urlParamTran(id); urlParamTran(names)); try { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); urlParamTran(dateStr)); } catch (Exception e) { e.printStackTrace(); } return JSON.toJSONString(urlParamTransfer); } @RequestMapping(path = "/bean3/{id}/{name}/{date}") @ResponseBody public Object bean3(HttpServletRequest request, @PathVariable("id") Integer id, @PathVariable("name") String name, @PathVariable("date") String dateStr) { UrlEncodedTransfer urlParamTransfer = new UrlEncodedTransfer(); urlParamTran(id); urlParamTran(name); try { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); urlParamTran(dateStr)); } catch (Exception e) { e.printStackTrace(); } return JSON.toJSONString(urlParamTransfer); } /** * 局部转换 * * @param binder * @param request */ @InitBinder public void initBinder(WebDataBinder binder, WebRequest request) { //转换日期 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // CustomDateEditor为自定义日期编辑器 binder.registerCustomEditor, new CustomDateEditor(dateFormat, true)); } @RequestMapping(path = "/bean4") @ResponseBody public Object bean4(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("name") String[] names, @RequestParam("date") Date date) { UrlEncodedTransfer urlParamTransfer = new UrlEncodedTransfer(); urlParamTran(id); urlParamTran(names)); urlParamTran(date); return JSON.toJSONString(urlParamTransfer); } @RequestMapping(path = "/bean5/{id}/{name}/{date}") @ResponseBody public Object bean5(HttpServletRequest request, @PathVariable("id") Integer id, @PathVariable("name") String name, @PathVariable("date") Date date) { UrlEncodedTransfer urlParamTransfer = new UrlEncodedTransfer(); urlParamTran(id); urlParamTran(name); urlParamTran(date); return JSON.toJSONString(urlParamTransfer); } @RequestMapping(path = "/bean6") @ResponseBody public Object bean6(HttpServletRequest request, UrlEncodedTransfer urlParamTransfer) { Sy(">>>>" + reque("Content-Type")); return JSON.toJSONString(urlParamTransfer); } }
package com.w.param; import lombok.Data; import java.u; @Data public class UrlEncodedTransfer { private Integer id; private String name; private Date date; }
功能测试:
#bean1 ;name=名称&name=名称2&date=1995-09-15 12:12:12 {"id":["123"],"name":["名称","名称2"],"date":["1995-09-15 12:12:12"]} #bean2 ;name=名称&name=名称2&date=1995-09-15 12:12:12 {"date":811138332000,"id":123,"name":"[名称, 名称2]"} #bean3 名称/1995-09-15 12:12:12 {"date":811138332000,"id":123,"name":"名称"} #bean4 ;name=名称&name=名称2&date=1995-09-15 12:12:12 {"date":811138332000,"id":123,"name":"[名称, 名称2]"} #bean5 名称/1995-09-15 12:12:12 {"date":811138332000,"id":123,"name":"名称"} #bean6 ;name=名称&date=1995-09-15 12:12:12&name=名称2 {"date":811138332000,"id":123,"name":"名称,名称2"}
Header头传递参数
功能描述:HTTP协议头传递参数,使用@RequestHeader接收参数。
package com.w; import com.alibaba.; import com.w.param.HeaderTransfer; import org.; import org..RequestHeader; import org..RequestMapping; import org..ResponseBody; import javax.; import javax.; import java.; import java.; import java.u; import java.u; import java.u; import java.u; @Controller("/headerTransfer") @RequestMapping("/headerTransfer") public class HeaderTransferController { @RequestMapping(path = "/bean1") @ResponseBody public Object bean1(HttpServletRequest request, HttpServletResponse response) { Map<String, Object> headerMap = new HashMap<>(); Enumeration<String> enumer = requeNames(); while ()) { String headName = (String) enumer.nextElement(); String headValue = reque(headName); (headName, headValue); } return JSON.toJSONString(headerMap); } @RequestMapping(path = "/bean2") @ResponseBody public Object bean2(HttpServletRequest request, @RequestHeader("id") Integer id, @RequestHeader("name") String[] names, @RequestHeader("date") String dateStr) { HeaderTransfer headerTransfer = new HeaderTransfer(); headerTran(id); headerTran(names)); try { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); headerTran(dateStr)); } catch (Exception e) { e.printStackTrace(); } return JSON.toJSONString(headerTransfer); } }
package com.w.param; import lombok.Data; import java.u; @Data public class HeaderTransfer { private Integer id; private String name; private Date date; }
功能测试:
#bean1 {"date":"1995-09-15 12:12:12","postman-token":"02e70588-c0c5-493b-bb08-97adcb7938ad","name":"name","host":"127.0.0.1:8080","connection":"keep-alive","id":"123","accept-encoding":"gzip, deflate, br","user-agent":"PostmanRuntime;,"accept":"*/*"} #bean2 {"date":811138332000,"id":123,"name":"[name]"}
POST请求(application/x-www-from-urlencoded)
功能描述:POST请求,Content-Type为application/x-www-from-urlencoded,使用: @RequestParam和对象接收参数。
package com.w; import com.alibaba.; import com.w.param.UrlEncodedBodyTransfer; import org.; import org.; import org.; import org..*; import org.; import javax.; import javax.; import java.; import java.; import java.u; import java.u; import java.u; @Controller("/urlEncodedBodyTransfer") @RequestMapping("/urlEncodedBodyTransfer") public class UrlEncodedBodyTransferController { @RequestMapping(path = "/bean1") @ResponseBody public Object bean1(HttpServletRequest request, HttpServletResponse response) { Map<String, String[]> paramsMap = reque(); if (paramsMap != null) { for (String key : ()) { Sy(key + "=" + Arrays.toString(key))); } } return JSON.toJSONString(paramsMap); } @RequestMapping(path = "/bean2") @ResponseBody public Object bean2(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("name") String[] names, @RequestParam("date") String dateStr) { UrlEncodedBodyTransfer urlEncodedBodyTransfer = new UrlEncodedBodyTransfer(); urlEncodedBodyTran(id); urlEncodedBodyTran(names)); try { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); urlEncodedBodyTran(dateStr)); } catch (Exception e) { e.printStackTrace(); } return JSON.toJSONString(urlEncodedBodyTransfer); } /** * 局部转换 * * @param binder * @param request */ @InitBinder public void initBinder(WebDataBinder binder, WebRequest request) { //转换日期 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // CustomDateEditor为自定义日期编辑器 binder.registerCustomEditor, new CustomDateEditor(dateFormat, true)); } @RequestMapping(path = "/bean3") @ResponseBody public Object bean3(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("name") String[] names, @RequestParam("date") Date date) { UrlEncodedBodyTransfer urlEncodedBodyTransfer = new UrlEncodedBodyTransfer(); urlEncodedBodyTran(id); urlEncodedBodyTran(names)); urlEncodedBodyTran(date); return JSON.toJSONString(urlEncodedBodyTransfer); } @RequestMapping(path = "/bean4") @ResponseBody public Object bean4(HttpServletRequest request, UrlEncodedBodyTransfer urlEncodedBodyTransfer) { Sy(">>>>" + reque("Content-Type")); // multipart/form-data; boundary=--------------------------8041549364281 // application/x-www-form-urlencoded return JSON.toJSONString(urlEncodedBodyTransfer); } }
package com.w.param; import lombok.Data; import java.u; /** * application/x-www-from-urlencoded */ @Data public class UrlEncodedBodyTransfer { private Integer id; private String name; private Date date; }
功能测试:
#bean1 {"id":["123"],"name":["名字1","名字2"],"date":["1995-09-15 12:12:12"]} #bean2 {"date":811138332000,"id":123,"name":"[名字1, 名字2]"} #bean3 {"date":811138332000,"id":123,"name":"[名字1, 名字2]"} #bean4 {"date":811138332000,"id":123,"name":"名字1,名字2"} 结论:@RequestBody,不支持:application/x-www-form-urlencoded @RequestMapping(path = "/bean5") @ResponseBody public Object bean5(HttpServletRequest request, @RequestBody UrlEncodedBodyTransfer urlEncodedBodyTransfer) { Sy(">>>>" + reque("Content-Type")); return JSON.toJSONString(urlEncodedBodyTransfer); } 报错: Resolved [org.: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
POST请求(multipart/form-data)
功能描述:POST请求,Content-Type为multipart/form-data,使用: @RequestParam和对象接收参数。
package com.w; import com.alibaba.; import com.w.param.FormDataTransfer; import org.; import org.; import org.; import org..*; import org.; import javax.; import javax.; import java.; import java.; import java.util.*; @Controller("/formDataTransfer") @RequestMapping("/formDataTransfer") public class FormDataTransferController { @RequestMapping(path = "/bean1") @ResponseBody public Object bean1(HttpServletRequest request, HttpServletResponse response) { Map<String, String[]> paramsMap = reque(); if (paramsMap != null) { for (String key : ()) { Sy(key + "=" + Arrays.toString(key))); } } return JSON.toJSONString(paramsMap); } @RequestMapping(path = "/bean2") @ResponseBody public Object bean2(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("name") String[] names, @RequestParam("date") String dateStr) { FormDataTransfer formDataTransfer = new FormDataTransfer(); formDataTran(id); formDataTran(names)); try { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); formDataTran(dateStr)); } catch (Exception e) { e.printStackTrace(); } return JSON.toJSONString(formDataTransfer); } /** * 局部转换 * * @param binder * @param request */ @InitBinder public void initBinder(WebDataBinder binder, WebRequest request) { //转换日期 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // CustomDateEditor为自定义日期编辑器 binder.registerCustomEditor, new CustomDateEditor(dateFormat, true)); } @RequestMapping(path = "/bean3") @ResponseBody public Object bean3(HttpServletRequest request, @RequestParam("id") Integer id, @RequestParam("name") String[] names, @RequestParam("date") Date date) { FormDataTransfer formDataTransfer = new FormDataTransfer(); formDataTran(id); formDataTran(names)); formDataTran(date); return JSON.toJSONString(formDataTransfer); } @RequestMapping(path = "/bean4") @ResponseBody public Object bean4(HttpServletRequest request, FormDataTransfer formDataTransfer) { Sy(">>>>" + reque("Content-Type")); // multipart/form-data; boundary=--------------------------8041549364281 // application/x-www-form-urlencoded return JSON.toJSONString(formDataTransfer); } @RequestMapping(path = "/bean6") @ResponseBody public Object bean6(HttpServletRequest request, HttpServletResponse response) { Sy("" + reque("Content-Type")); Map<String, Object> paramsMap = new HashMap<>(); // return JSON.toJSONString(paramsMap); } }
package com.w.param; import lombok.Data; import java.u; /** * multipart/form-data */ @Data public class FormDataTransfer { private Integer id; private String name; private Date date; }
功能测试:
#bean1 {"id":["123"],"name":["名字1","名字2"],"date":["1995-09-15 12:12:12"]} #bean2 {"date":811138332000,"id":123,"name":"[名字1, 名字2]"} #bean3 {"date":811138332000,"id":123,"name":"[名字1, 名字2]"} #bean4 {"date":811138332000,"id":123,"name":"名字1,名字2"} 结论:@RequestBody,不支持:multipart/form-data @RequestMapping(path = "/bean5") @ResponseBody public Object bean5(HttpServletRequest request, @RequestBody FormDataTransfer formDataTransfer) { Sy(">>>>" + reque("Content-Type")); return JSON.toJSONString(formDataTransfer); } 报错: Resolved [org.: Content type 'multipart/form-data;boundary=--------------------------0340280710906;charset=UTF-8' not supported]
POST请求(application/json)
功能描述:POST请求,Content-Type为application/json,使用: @RequestBody接收参数。
package com.w; import com.alibaba.; import com.w.param.JsonBodyTransfer; import org.; import org..RequestBody; import org..RequestMapping; import org..RequestParam; import org..ResponseBody; import javax.; import javax.; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @Controller("/jsonBodyTransfer") @RequestMapping("/jsonBodyTransfer") public class JsonBodyTransferController { @RequestMapping(path = "/bean1") @ResponseBody public Object bean1(HttpServletRequest request, HttpServletResponse response) { // JsonBodyTransfer jsonBodyTransfer = JSON.parseObject(toJson(request), J); return jsonBodyTransfer; } /** * @param request * @return */ private String toJson(HttpServletRequest request) { String result = ""; try { InputStream input = reque(); InputStreamReader reader = new InputStreamReader(input); BufferedReader bufferReader = new BufferedReader(reader); String line = null; StringBuilder sb = new StringBuilder(); while ((line = bu()) != null) { (line); } result = (); } catch (IOException e) { e.printStackTrace(); } return result; } @RequestMapping(path = "/bean2") @ResponseBody public Object bean2(HttpServletRequest request, @RequestBody JsonBodyTransfer jsonBodyTransfer) { return jsonBodyTransfer; } }
package com.w.param; import com.alibaba.; import com.; import lombok.Data; import java.u; /** * application/json */ @Data public class JsonBodyTransfer { private Integer id; private String name; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date date; public static void main(String[] args) { JsonBodyTransfer jsonBodyTransfer = new JsonBodyTransfer(); jsonBodyTran(1212); jsonBodyTran("名称"); jsonBodyTran(new Date()); Sy(jsonBodyTransfer)); } }
功能测试:
#bean1 --------------------------------------------------------------------- { "date":"1995-09-15 12:12:12", "id":1212, "name":"名称" } --------------------------------------------------------------------- { "id": 1212, "name": "名称", "date": "1995-09-15T04:12:12.000+00:00" } --------------------------------------------------------------------- #bean2 --------------------------------------------------------------------- { "date":"1995-09-15 12:12:12", "id":1212, "name":"名称" } --------------------------------------------------------------------- { "id": 1212, "name": "名称", "date": "1995-09-15 12:12:12" } ---------------------------------------------------------------------
POST请求(application/xml)
功能描述:POST请求,Content-Type为application/xml,使用:流接收参数。
package com.w; import org.; import org..RequestMapping; import org..ResponseBody; import javax.; import javax.; import javax.; import java.io.IOException; @Controller("/xmlBodyTransfer") @RequestMapping("/xmlBodyTransfer") public class XmlBodyTransferController { @RequestMapping(path = "/bean1") @ResponseBody public Object bean1(HttpServletRequest request, HttpServletResponse response) { Sy(">>>>" + reque("Content-Type")); byte[] readBytes = new byte[0]; try { readBytes = readBytes(request); } catch (IOException e) { e.printStackTrace(); } return new String(readBytes); } /** * @param request * @return * @throws IOException */ private byte[] readBytes(HttpServletRequest request) throws IOException { int len = reque(); ServletInputStream input = reque(); byte[] buffer = new byte[len]; in(buffer, 0, len); return buffer; } }
功能测试:
#bean1 --------------------------------------------------------------------- <root> <id>1234</id> <name>代码</name> </root> --------------------------------------------------------------------- <root> <id>1234</id> <name>代码</name> </root> ---------------------------------------------------------------------