ios前端比较懒~囧~~,还有已安装的app用户 ,不愿意更新等问题 导致 请求头无法补全给服务端springmvc发送过来。。。。。悲剧了
只能服务端自己处理这个不全的http请求空参无法解析的问题
尝试处理方法
1 增加springmvc过滤器对请求进行过滤(失败)
测试发现在访问控制层匹配请求地址之前就已经报错,应该是dispatcher那里出了问题
虽然失败, 把代码配置贴出来~~
applicationContext.xml
<mvc:interceptors>
<mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.interceptor.ForIosParam"></bean> </mvc:interceptor> </mvc:interceptors>ForIosParam.java
public class ForIosParam implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { StringBuffer url=request.getRequestURL(); Map requestParams=request.getParameterMap(); Method method=requestParams.getClass().getMethod("setLocked",new Class[]{boolean.class}); method.invoke(requestParams,new Object[]{new Boolean(false)}); requestParams.put("ForIosParam", "ForIosParam"); Map map1=request.getParameterMap(); request.getRequestURL().append("?ForIosParam=ForIosParam"); request.setAttribute("ForIosParam", "ForIosParam"); StringBuffer url1=request.getRequestURL(); return true; }}2 使用servlet在接收到请求之后发送再次自己给自己发送一个http完整的请求 (失败,为毛请求发出去了,然后就没有然后了一点反应也木有。。。。必须异步的吗? 不管那么多了。。。换)这个网上搜就可以java发送http post请求
3 使用 servlet利用post在接收到请求之后,增加参数再服务器跳转forward到springmvc 的get方式接收的方法里(失败啊。。。。。每次都是servlet拦截到。。。。。悲催 为毛 不管那么多 。。。。换)
代码如下
package com.util;
import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.UUID;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import com.model.umaiw_app_system;import com.service.GetInitKeyService;import com.service.GetTheBestServerService;import com.service.implement.GetInitKeyServiceImpl;import com.service.implement.GetTheBestServerServiceImpl;import com.test.HttpRequest;public class getTheBestServerServlet extends HttpServlet{ public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/json; charset=gb2312"); Map requestParams=request.getParameterMap(); Method method; try { method = requestParams.getClass().getMethod("setLocked",new Class[]{boolean.class}); method.invoke(requestParams,new Object[]{new Boolean(false)}); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } requestParams.put("ForIosParam", "ForIosParam"); Map map1=request.getParameterMap(); request.getRequestURL().append("?ForIosParam=ForIosParam"); request.setAttribute("ForIosParam", "ForIosParam"); ServletContext sc = getServletContext(); RequestDispatcher rd = null; rd = sc.getRequestDispatcher(request.getRequestURI()); //定向的页面 rd.forward(request, response); StringBuffer url=request.getRequestURL();// response.sendRedirect(url.toString()); } }5 干脆 直接用servlet吧无参的http请求处理掉(成功啦~~ )
不过这种方式因为是单独走的servlet没有走spring mvc和spring框架的支持(木有jdbcTemplate,木有自动注入,木有数据库连接池~~~OMG 对付吧 ,不管三七二十一拼了,谁让他不补全标准的http请求哒)
代码如下(不能注入就实例化)
package com.util;
import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.UUID;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import com.model.umaiw_app_system;import com.service.GetInitKeyService;import com.service.GetTheBestServerService;import com.service.implement.GetInitKeyServiceImpl;import com.service.implement.GetTheBestServerServiceImpl;import com.test.HttpRequest;public class getTheBestServerServlet extends HttpServlet{ public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { GetInitKeyServiceImpl getInitKeyService=new GetInitKeyServiceImpl(); GetTheBestServerServiceImpl getTheBestServerService= new GetTheBestServerServiceImpl(); Map<String, String> map = new HashMap<String, String>(); map.put("request", "getTheBestServer"); map.put("status", "0");//阿里云获取真实ip,这个也让人头疼。。。。。
String ip=request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } String initKey =""; umaiw_app_system uas = getInitKeyService.getinitKey(ip); if ( null== uas ) { String uuid = UUID.randomUUID().toString(); initKey = uuid.substring(uuid.length()-8); String uuidkey = UUID.randomUUID().toString(); String key = uuidkey.substring(uuidkey.length()-8); try { getTheBestServerService.insertKey(ip, initKey, new Date(),key); } catch (Exception e) { map.put("status", "1"); } } else { initKey=uas.getInit_key(); } try { map.put("serverIp", DesUtil.encrypt( "XX.XX.XX.XX"(可以使负载均衡的ip),initKey)); } catch (Exception e) { e.printStackTrace(); } map.put("initKey", initKey); map.put("ip", ip); JSONObject jsonObject = JSONObject.fromObject(map); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(jsonObject.toString()); }}web.xml
<servlet>
<servlet-name>getTheBestServerServlet</servlet-name> <servlet-class>com.util.getTheBestServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>getTheBestServerServlet</servlet-name> <url-pattern>/system/getTheBestServer</url-pattern> </servlet-mapping>数据库访问层用最基本的jdbc访问(别忘了使用完连接 关了它,,,,,)
附上 阿里云获取真实ip的tomcat下
server.xml最后一行的配置
<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="%{X-Forwarded-For}i %h %l %u %t "%r" %s %b" suffix=".txt" prefix="localhost_access_log." directory="logs"/>
大功告成 这样就可以单独处理无参不全请求头的死ios的http请求啦。。。。。
如果是json字符串请求服务器端的话 springmvc默认 是text/plian这种键值对的接收方式 (也就是浏览器发送过来的形式)
但是 如果是 application/json这种的contenttype的话 是接收不到参数的 ,(在拦截器里设置请求的请求的contenttype也不行)只好让手机端改成键值对的形式