Spring mvc Controller 处理安卓post和get请求
2017-12-19 10:34
513 查看
@Controller
@RequestMapping ("/MyController")
public class MyController {
@RequestMapping ("/test")
@ResponseBody
public String download (HttpServletRequest request)
{
String pathname = request.getParameter("a");
String filename = request.getParameter("b");
return pathname+";"+filename;
}
@RequestMapping (value = "/test1",method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> download1 (HttpServletRequest request)
{
String pathname = request.getParameter("a");
String filename = request.getParameter("b");
HashMap hashMap = new HashMap();
hashMap.put("a", pathname);
hashMap.put("b", filename);
return hashMap;
}
@RequestMapping (value ="/test2",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> download2 (HttpServletRequest request)
{
Map<String, Object> httpToMap = HttpToMap(request);
if(httpToMap== null) {
httpToMap = new HashMap<String,Object>();
httpToMap.put("error", "没有数据");
}
return httpToMap;
}
@RequestMapping (value ="/test3",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> download3 (HttpServletRequest request)
{
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String parameter = request.getParameter("a");
return null;
}
StudentDAO studentDAO = new StudentDAO();//调用登陆判断方法
@RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){
System.out.println("拦截了客户端json请求");
Map<String, Object> map = new HashMap<String, Object>();
System.out.println("密码错误");
map.put("result", "密码错误");
return map; //封装为json返回给客户端
}
//********************************************************************************
public Map <String, Object> HttpToMap (HttpServletRequest request)
{
Map <String, Object> map;
try
{
JSONObject jsonObject = HttpToJson (request);
map = getMap (jsonObject);
}
catch (Exception e)
{
map = null;
}
return map;
}
public JSONObject HttpToJson (HttpServletRequest request)
{
JSONObject jsonObject;
BufferedReader reader = null;
try
{
reader = new BufferedReader (new InputStreamReader (request.getInputStream (), "utf-8"));
String jsonStr = null;
StringBuilder result = new StringBuilder ();
while ((jsonStr = reader.readLine ()) != null)
{
result.append (jsonStr);
}
jsonObject = JSONObject.parseObject (result.toString ()); // 取一个json转换为对象
}
catch (Exception e)
{
jsonObject = null;
}
finally
{
if (null != reader)
{
try
{
reader.close ();
}
catch (IOException e)
{
//LOGGER.error ("Close reader error : ", e);
} // 关闭输入流
}
}
return jsonObject;
}
private Map <String, Object> getMap (JSONObject jsonObject)
{
Map <String, Object> map = new HashMap <String, Object> ();
for (Iterator <String> keyIter = jsonObject.keySet ().iterator (); keyIter.hasNext ();)
{
String key = (String) keyIter.next ();
if (jsonObject.get (key).getClass ().equals (JSONObject.class))
{
Map <String, Object> map2;
map2 = getMap (jsonObject.getJSONObject (key));
map.put (key, map2);
}
else if (jsonObject.get (key).getClass ().equals (JSONArray.class))
{
List <Map <String, Object>> list;
list = getList (jsonObject.getJSONArray (key));
map.put (key, list);
}
else
{
map.put (key, jsonObject.get (key));
}
}
return map;
}
private List <Map <String, Object>> getList (JSONArray jsonArray)
{
List <Map <String, Object>> list = new ArrayList<> ();
for (int i = 0; i < jsonArray.size (); i++)
{
Map <String, Object> map2;
map2 = getMap (jsonArray.getJSONObject (i));
list.add (map2);
}
return list;
}
}
以上代码:
MyController 类之前加入注解
@Controller 声明是controller层
@RequestMapping ("/MyController") 该controller层目录结构 访问路径 ip:端口号/工程名称/MyController/
访问路径为:
ip:端口号/工程名称/MyController/test?a=username&b=passworld
get请求内容在请求头上,拼接在url后面
如果不声明请求方式默认为get请求,所以 test和test1请求是等效的
这种方式自定义格式
访问路径为:
ip:端口号/工程名称/MyController/doLogin/username/passworld
就可以得到 username 和passworld字段
2.post请求
post请求请求的内容通过http body部分发送
这里通过request拿到流对象,再转换成String -->Json -->Map 参考工具方法:HttpToMap,HttpToJson,getMap,getList方法
以上方法亲测可用
test3,本来
request.getParameter("key值"),可以拿到json的值,或者
request.getParameterMap可以拿到集合 这里一直拿到空(待研究)
@RequestMapping ("/MyController")
public class MyController {
@RequestMapping ("/test")
@ResponseBody
public String download (HttpServletRequest request)
{
String pathname = request.getParameter("a");
String filename = request.getParameter("b");
return pathname+";"+filename;
}
@RequestMapping (value = "/test1",method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> download1 (HttpServletRequest request)
{
String pathname = request.getParameter("a");
String filename = request.getParameter("b");
HashMap hashMap = new HashMap();
hashMap.put("a", pathname);
hashMap.put("b", filename);
return hashMap;
}
@RequestMapping (value ="/test2",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> download2 (HttpServletRequest request)
{
Map<String, Object> httpToMap = HttpToMap(request);
if(httpToMap== null) {
httpToMap = new HashMap<String,Object>();
httpToMap.put("error", "没有数据");
}
return httpToMap;
}
@RequestMapping (value ="/test3",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> download3 (HttpServletRequest request)
{
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String parameter = request.getParameter("a");
return null;
}
StudentDAO studentDAO = new StudentDAO();//调用登陆判断方法
@RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){
System.out.println("拦截了客户端json请求");
Map<String, Object> map = new HashMap<String, Object>();
System.out.println("密码错误");
map.put("result", "密码错误");
return map; //封装为json返回给客户端
}
//********************************************************************************
public Map <String, Object> HttpToMap (HttpServletRequest request)
{
Map <String, Object> map;
try
{
JSONObject jsonObject = HttpToJson (request);
map = getMap (jsonObject);
}
catch (Exception e)
{
map = null;
}
return map;
}
public JSONObject HttpToJson (HttpServletRequest request)
{
JSONObject jsonObject;
BufferedReader reader = null;
try
{
reader = new BufferedReader (new InputStreamReader (request.getInputStream (), "utf-8"));
String jsonStr = null;
StringBuilder result = new StringBuilder ();
while ((jsonStr = reader.readLine ()) != null)
{
result.append (jsonStr);
}
jsonObject = JSONObject.parseObject (result.toString ()); // 取一个json转换为对象
}
catch (Exception e)
{
jsonObject = null;
}
finally
{
if (null != reader)
{
try
{
reader.close ();
}
catch (IOException e)
{
//LOGGER.error ("Close reader error : ", e);
} // 关闭输入流
}
}
return jsonObject;
}
private Map <String, Object> getMap (JSONObject jsonObject)
{
Map <String, Object> map = new HashMap <String, Object> ();
for (Iterator <String> keyIter = jsonObject.keySet ().iterator (); keyIter.hasNext ();)
{
String key = (String) keyIter.next ();
if (jsonObject.get (key).getClass ().equals (JSONObject.class))
{
Map <String, Object> map2;
map2 = getMap (jsonObject.getJSONObject (key));
map.put (key, map2);
}
else if (jsonObject.get (key).getClass ().equals (JSONArray.class))
{
List <Map <String, Object>> list;
list = getList (jsonObject.getJSONArray (key));
map.put (key, list);
}
else
{
map.put (key, jsonObject.get (key));
}
}
return map;
}
private List <Map <String, Object>> getList (JSONArray jsonArray)
{
List <Map <String, Object>> list = new ArrayList<> ();
for (int i = 0; i < jsonArray.size (); i++)
{
Map <String, Object> map2;
map2 = getMap (jsonArray.getJSONObject (i));
list.add (map2);
}
return list;
}
}
以上代码:
MyController 类之前加入注解
@Controller 声明是controller层
@RequestMapping ("/MyController") 该controller层目录结构 访问路径 ip:端口号/工程名称/MyController/
1.get请求
@RequestMapping ("/test") @ResponseBody public String download (HttpServletRequest request) { String pathname = request.getParameter("a"); String filename = request.getParameter("b"); return pathname+";"+filename; }
访问路径为:
ip:端口号/工程名称/MyController/test?a=username&b=passworld
get请求内容在请求头上,拼接在url后面
如果不声明请求方式默认为get请求,所以 test和test1请求是等效的
StudentDAO studentDAO = new StudentDAO();//调用登陆判断方法 @RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET) @ResponseBody public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){ System.out.println("拦截了客户端json请求"); Map<String, Object> map = new HashMap<String, Object>(); System.out.println("密码错误"); map.put("result", "密码错误 9116 "); return map; //封装为json返回给客户端 }
这种方式自定义格式
访问路径为:
ip:端口号/工程名称/MyController/doLogin/username/passworld
就可以得到 username 和passworld字段
2.post请求
post请求请求的内容通过http body部分发送
@RequestMapping (value ="/test2",method = RequestMethod.POST) @ResponseBody public Map<String, Object> download2 (HttpServletRequest request) { Map<String, Object> httpToMap = HttpToMap(request); if(httpToMap== null) { httpToMap = new HashMap<String,Object>(); httpToMap.put("error", "没有数据"); } return httpToMap; }
这里通过request拿到流对象,再转换成String -->Json -->Map 参考工具方法:HttpToMap,HttpToJson,getMap,getList方法
以上方法亲测可用
test3,本来
request.getParameter("key值"),可以拿到json的值,或者
request.getParameterMap可以拿到集合 这里一直拿到空(待研究)
相关文章推荐
- SpringMVC Controller接收前台ajax的GET或POST请求返回各种参数(转)
- SpringMVC JQuery Ajax Get Post请求在Tomcat中乱码解决方案
- spring mvc controller 各注解GET和POST 使用说明
- spring+mvc get请求,post请求编码问题(utf-8)
- Spring MVC @RestController接收POST请求,用Map接收
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- Spring MVC Controller与jquery ajax请求处理json
- spring boot get和post请求,以及requestbody为json串时候的处理
- Spring Boot get和post请求,以及requestbody为json串时候的处理
- java springmvc controller raw xml post request.getInputStream();
- SpringMVC JQuery Ajax Get Post请求在Tomcat中乱码解决方案
- spring boot get和post请求,以及requestbody为json串时候的处理
- 如何处理HTTP POST/GET请求
- 后台处理get与post请求
- SpringBoot构建Restful service完成Get和Post请求
- java springmvc post和get中文乱码问题
- Spring3.1.0实现原理分析(十七).MVC请求处理方法参数解析器和返回值解析器
- springmvc controller 统一异常处理
- 安卓基础-get post请求与提交数据
- SpringWeb MVC处理请求的流程:(处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件)