springmvc中,关于context-type返回text/plain说明

springmvc中,关于context-type的text/plain说明

  • 当方法上加@ResponseBody注解的时候,返回的数据的context-type'一定是application/json;类型的,即使指定了@requestMapping中的produces属性的值也无效。
  • 如果context-type'希望是指定类型,那么请参考一下写法
@RequestMapping(value = "resolveData")
	@ResponseBody
	public void resolveData(@ModelAttribute ZncdModel model,HttpServletResponse response) throws IOException {
		response.setContentType("text/plain;charset=UTF-8");//指定类型
		Map<String, String> result = new HashMap<>();
		result.put("eventid", JFunction.getUUID_16());
		result.put("status", "OK");
		response.getWriter().write(result.toString());//用流直接输出
	}
  • 当请求体的context-typetext/plain时,接受参数的方法如下:
// 接收请求数据
			BufferedReader reader = request.getReader();
			char[] buf = new char[512];
			int len = 0;
			StringBuffer contentBuffer = new StringBuffer();
			while ((len = reader.read(buf)) != -1) {
			    contentBuffer.append(buf, 0, len);
			}
			content = contentBuffer.toString();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章