我正在尝试创建2个项目,一个是“客户端”站点,一个是“服务器”站点。
我的项目是关于Client调用的,带有param的方法是UserDTO。服务器站点将更改该UserDTO的userName。
这是服务器站点中的文件web.xml:
<web-app version="2.5"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>httpInvoker</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>httpInvoker</servlet-name>
<url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>
服务器站点中的文件httpInvoker-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="detailsUser" class="com.higgsup.internship.rmi.DetailsUserImpl"></bean>
<bean name="/DetailsUser.http" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="detailsUser"></property>
<property name="serviceInterface" value="com.higgsup.internship.rmi.DetailsUser"></property>
</bean>
</beans>
类详细信息用户:
public interface DetailsUser {
public int show(UserDTO userDTO);
}
类的详细信息
public class DetailsUserImpl implements DetailsUser {
public int show(UserDTO userDTO) {
return 1;
}
}
在两个站点中对UserDTO进行分类:
public class UserDTO {
public String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
并且在客户站点上有:
文件client-beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="detailsUser" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/DetailsUser.http"></property>
<property name="serviceInterface" value="com.higgsup.internship.rmi.DetailsUser"></property>
</bean>
</beans>
类客户:
public class Client {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
UserDTO userDTO = new UserDTO();
DetailsUser detailsUser = (DetailsUser)context.getBean("detailsUser") ;
detailsUser.show(userDTO);
}
}
启动服务器站点时的日志:
> Connected to server
[2016-07-22 09:41:44,055] Artifact httpinvoke:war exploded: Artifact is being deployed, please wait...
22-Jul-2016 21:41:45.372 INFO [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
22-Jul-2016 21:41:45.572 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'httpInvoker': initialization started
22-Jul-2016 21:41:45.609 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'httpInvoker-servlet': startup date [Fri Jul 22 21:41:45 ICT 2016]; root of context hierarchy
22-Jul-2016 21:41:45.669 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from ServletContext resource [/WEB-INF/httpInvoker-servlet.xml]
22-Jul-2016 21:41:46.144 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping.registerHandler Mapped URL path [/DetailsUser.http] onto handler '/DetailsUser.http'
22-Jul-2016 21:41:46.298 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'httpInvoker': initialization completed in 726 ms
[2016-07-22 09:41:46,328] Artifact httpinvoke:war exploded: Artifact is deployed successfully
[2016-07-22 09:41:46,328] Artifact httpinvoke:war exploded: Deploy took 2,273 milliseconds
22-Jul-2016 21:41:53.843 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory D:\Setup\tomcat\apache-tomcat-9.0.0.M4\webapps\manager
22-Jul-2016 21:41:53.873 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory D:\Setup\tomcat\apache-tomcat-9.0.0.M4\webapps\manager has finished in 29 ms
和启动客户端站点时的日志:
> Jul 22, 2016 9:42:24 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1b8d17c: startup date [Fri Jul 22 21:42:24 ICT 2016]; root of context hierarchy
Jul 22, 2016 9:42:24 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [client-beans.xml]
Exception in thread "main" org.springframework.remoting.RemoteAccessException: Could not access HTTP invoker remote service at [http://localhost:8080/DetailsUser.http]; nested exception is java.io.NotSerializableException: com.higgsup.internship.rmi.UserDTO
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.convertHttpInvokerAccessException(HttpInvokerClientInterceptor.java:216)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy1.show(Unknown Source)
at com.higgsup.internship.rmi.Client.main(Client.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.io.NotSerializableException: com.higgsup.internship.rmi.UserDTO
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1378)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.doWriteRemoteInvocation(AbstractHttpInvokerRequestExecutor.java:200)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.writeRemoteInvocation(AbstractHttpInvokerRequestExecutor.java:169)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.getByteArrayOutputStream(AbstractHttpInvokerRequestExecutor.java:149)
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.executeRequest(AbstractHttpInvokerRequestExecutor.java:133)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:194)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:176)
at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:144)
... 9 more
Process finished with exit code 1
服务器站点的代码构造函数:
客户端站点的代码构造函数:
最佳答案
只需添加公共类UserDTO,即可实现Serializable来修复此特定异常。
关于java - 如何设置Http调用程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38529691/