后台配置及代码
<bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="method"></property>
</bean>
<!-- 下边配置请求 -->
<bean name="/busi/userInfo.action" class="com.mxci.busi.control.impl.UserInfoControllerImpl">
<property name="methodNameResolver">
<ref bean="paramMethodResolver"/><!-- 引用上边那个方法名称解析器 -->
</property>
</bean>
public String regist(HttpServletRequest request, HttpServletResponse response, UserInfo userInfo) {
// TODO Auto-generated method stub
LOGGER.info("[CR]商户注册开始.请求商户信息:"+userInfo);
MxciInsideResult mxciInsideResult = userInfoService.busiCustRegist(userInfo);//调用
//组装响应数据,并初始化进MxciResponseBody里面
MxciResponseServiceImpl mxciResponseService
= new MxciResponseServiceImpl(mxciInsideResult.getRecode(),mxciInsideResult.getReData(),response);
mxciResponseService.response();//响应结果到客户端
mxciResponseService=null;//置空
return null;
}
目前请求地址这样写是可以封装层userId对象的
http://127.0.0.1:8090/mxciBusiness/busi/userInfo.action?method=regist&userId=wangyan&password=900613&certificateId=50022419901231739X&phoneNo=18696668026
请问怎么样修改才能用下边的地址访问能让后台接收到对象呢???
http://127.0.0.1:8090/mxciBusiness/busi/userInfo.action?method=regist&userInfo={"userId": "wangyan888","customerId": "BUSI20160125000001","lastErrorTime": null,"loginDate": null,"loginErrNum": 0,"password": "","phoneNo": "18696668026","reginDate": null,"status": 0,"valiDate":null}
可以以 String 形式去接收,然后用fastjson 去格式化成json对象。 感兴趣可以去看一看fastjson
我想知道为什么要这样子传值过去
这样传值号奇葩,,思路偏了吧
jQuery用ajax想后台传递参数的时候把参数设置为{"userId": "wangyan888","customerId": "BUSI20160125000001","lastErrorTime": null,"loginDate": null,"loginErrNum": 0,"password": "","phoneNo": "18696668026","reginDate": null,"status": 0,"valiDate":null}命名为sdata,在ajax的data参数上使用JSON.stringify(sdata)方法把sdata格式转换就好了,controller里接收的对象加上@RequestBody
可以通过@RequestBody注解来实现
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@RequestMapping("/")
@ResponseBody
public JsonDto someFunction(@RequestBody Dto dto) {
return null;
}
xml 里面也需要配置一下支持 json
我的配置可能有点老了
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
<entry key="htm" value="text/html"/>
<entry key="jsp" value="text/html" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true"/>
</bean>
使用的是Spring 4.1.7.RELEASE