WebService Restful方式 Post请求Java无法获取到前台请求的参数,请大牛帮忙!!!
16lz
2021-01-22
这2天学习搭建WebServiceRestful,前台发送Post请求的参数Service层中一直无法获取参数。。
JDK:1.7
apache-tomcat-7.0.59
CXF2.4
Spring3.1
web.xml
service.xml
applicationContext.xml
UserVO
IUserService接口
UserService实现类
index.html
我在公司的项目里定义的接口方法Post可以拿VO接收前台请求的JSON字符,可以自动转化成VO自己搭建就不可以请求成功了但VO的属性都是空的是少了什么配置或者哪里错误了吗?请大牛们指导指导纠结好久了。。。
goodluck
JDK:1.7
apache-tomcat-7.0.59
CXF2.4
Spring3.1
web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"version="3.0">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
service.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">
<importresource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<beanid="jsonProvider"class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
<jaxrs:serverid="userServiceRest"address="/service">
<jaxrs:serviceBeans>
<refbean="userService"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<refbean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
</beans>
applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<importresource="service.xml"/>
<context:component-scanbase-package="service"/>
<!--JNDI外部名称-->
<beanid="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">
<propertyname="jndiName">
<value>java:comp/env/jdbc/myoracle</value>
</property>
</bean>
<!--配置事务管理器-->
<beanname="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--配置事务切面-->
<aop:configproxy-target-class="true">
<aop:advisorpointcut="execution(public*service.*.*(..))"advice-ref="txAdvice"/>
</aop:config>
<!--配置切面方法及使用的事务-->
<tx:adviceid="txAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="find*"read-only="true"/>
<tx:methodname="get*"read-only="true"/>
<tx:methodname="query*"read-only="true"/>
<tx:methodname="*"read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置Mybatis的SqlSessionFactory-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--配置MyBatisXML与DAO的映射-->
<beanid="userMapper"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="dao"/>
<propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>
</bean>
</beans>
UserVO
packageentityVO;
publicclassUserVO{
privateIntegerid;
privateStringusername;
privateStringpassword;
publicIntegergetId(){
returnid;
}
publicvoidsetId(Integerid){
this.id=id;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringtoString(){
return"UserVO[id="+id+",username="+username+",password="
+password+"]";
}
}
IUserService接口
packageservice;
importjava.util.List;
importjavax.ws.rs.GET;
importjavax.ws.rs.POST;
importjavax.ws.rs.Path;
importjavax.ws.rs.PathParam;
importjavax.ws.rs.Produces;
importjavax.ws.rs.QueryParam;
importjavax.ws.rs.core.MediaType;
importentityVO.UserVO;
@Path("/userService")
@Produces(MediaType.APPLICATION_JSON)
publicinterfaceIUserService{
@GET
@Path("/findUserInfo")
publicUserVOfindUserInfo(@QueryParam("")UserVOuser);
@GET
@Path("/findUser/{username}/{password}")
publicUserVOfindUser(@PathParam("")UserVOuser);
@POST
@Path("/findUserList/{username}")
publicList<UserVO>findUserList(@QueryParam("")UserVOuser,@PathParam("username")StringuserName);
@POST
@Path("/findUserList")
publicList<UserVO>findUserList(@QueryParam("")UserVOuser);
@POST
@Path("/findUserList2")
publicList<UserVO>findUserList2(UserVOuser);
}
UserService实现类
packageservice.impl;
importjava.util.ArrayList;
importjava.util.List;
importjavax.inject.Inject;
importjavax.inject.Named;
importservice.IUserService;
importdao.IUserDao;
importentityVO.UserVO;
@Named
publicclassUserServiceimplementsIUserService{
@Inject
IUserDaouserDao;
UserVOuserVO=newUserVO();
UserService(){
userVO.setId(1);
userVO.setPassword("密码");
userVO.setUsername("账号");
}
publicUserVOfindUserInfo(UserVOuser){
System.out.println(user);
returnuserVO;
}
publicUserVOfindUser(UserVOuser){
System.out.println(user);
returnuserVO;
}
publicList<UserVO>findUserList(UserVOuser,StringuserName){
System.out.println(user);
System.out.println(userName);
List<UserVO>userList=newArrayList<UserVO>();
for(inti=0;i<15;i++){
userList.add(userVO);
}
returnuserList;
}
publicList<UserVO>findUserList(UserVOuser){
System.out.println(user);
List<UserVO>userList=newArrayList<UserVO>();
for(inti=0;i<15;i++){
userList.add(userVO);
}
returnuserList;
}
publicList<UserVO>findUserList2(UserVOuser){
System.out.println(user);
List<UserVO>userList=newArrayList<UserVO>();
for(inti=0;i<15;i++){
userList.add(userVO);
}
returnuserList;
}
}
index.html
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<scripttype="text/javascript"src="js/jquery-1.7.2.js"></script>
<scripttype="text/javascript">
$(document).ready(function(){
$.ajax({
/*url:"services/service/userService/findUserList",*/
url:"services/service/userService/findUserList2",
/*url:"services/service/userService/findUserList/张三",*/
type:"POST",
data:{username:"张三",password:"密码"},
contentType:"application/json",
aync:false,
/*processData:false, //设置processData选项为false,防止自动转换数据格式*/
success:function(result){
/*debugger;
console.log(result);*/
}
});
});
</script>
<title>Inserttitlehere</title>
</head>
<body>
<h1id="result"></h1>
</body>
</html>
我在公司的项目里定义的接口方法Post可以拿VO接收前台请求的JSON字符,可以自动转化成VO自己搭建就不可以请求成功了但VO的属性都是空的是少了什么配置或者哪里错误了吗?请大牛们指导指导纠结好久了。。。
7 个解决方案
#1
goodluck
更多相关文章
- HTML H5之ASCII 代码转义字符集实体编号
- Mysql字符集和校验规则
- MySQL数据库总结(8)字符集与校对集
- mysql字符集浅谈
- mysql字符集设置
- Mysql迁移由于字符集导致乱码的数据
- Mysql基础之字符集与乱码
- JavaScript正则表达式定义字符集
- 【整理】更改MSSQL默认字符集