Search code examples
javaspringhttpinvoker

How to setup Http invoker?


I'm trying to create 2 projects, one is Client site, and one is Server site.

My projects is about Client call a method with param is a UserDTO. And Server site will change userName of that UserDTO.

This is file web.xml in Server site:

<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>

File httpInvoker-servlet.xml in Server site:

<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>

Class DetailsUser:

public interface DetailsUser {
    public int show(UserDTO userDTO);
}

Class DetailsUserImpl:

public class DetailsUserImpl implements DetailsUser {

public int show(UserDTO userDTO) {
    return 1;
}
}

Class UserDTO in both sites:

public class UserDTO {
public String userName;

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}
}

And in the Client site has:

file 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>

Class Client:

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);
}
}

The log when I start Server site:

> 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

And the log when I start Client site:

> 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

Code structor of Server site:

enter image description here

And code structor of Client site:

enter image description here


Solution

  • Just add public class UserDTO implements Serializable to fix this specific exception.