1

I don't understand quite well how to configure web.xml file for Spring Http Invoker if I have a java config.

Here is config:

@Configuration
@Import(JdbcConfiguration.class)
@EnableWebMvc
public class HttpInvokerConfig {

    @Bean
    public HttpInvokerServiceExporter contactExporter(){
        HttpInvokerServiceExporter contactExporter = new HttpInvokerServiceExporter();
        contactExporter.setServiceInterface(ContactDao.class);
        return contactExporter;
    }

    @Bean
    public HttpInvokerProxyFactoryBean remoteContactService(){
        HttpInvokerProxyFactoryBean remoteContactService = new HttpInvokerProxyFactoryBean();
        remoteContactService.setServiceUrl("http://localhost:8080/experimental/ContactService");
        remoteContactService.setServiceInterface(ContactDao.class);
        return remoteContactService;
    }

}

Here web.xml:

<web-app 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_3_0.xsd"
         version="3.0">

    <display-name>Spring HTTP Invoker Sample</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>contactExporter</servlet-name>

        <servlet-class>
            org.springframework.web.context.support.HttpRequestHandlerServlet
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>contactExporter</servlet-name>
        <url-pattern>/remoting/ContactService</url-pattern>
    </servlet-mapping>
</web-app>

What do I need to specify in context-param instead of path to xml and is it possible to run the invoker without web.xml(like servlet 3.0 spec)?

Alex Bondar
  • 1,167
  • 4
  • 18
  • 35
  • Take a look at [AbstractAnnotationConfigDispatcherServletInitializer](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.html), extend this class and your class will work as an xml equivalent of `web.xml` file. – Prabhat Jul 19 '16 at 10:54
  • For more information check this [post](http://stackoverflow.com/questions/26676782/when-use-abstractannotationconfigdispatcherservletinitializer-and-webapplication). – Prabhat Jul 19 '16 at 11:02

1 Answers1

0

Your configuration is almost right. What you are missing are two things:

  • The bean name of the HttpInvokerProxyFactoryBean must specify the URL the exporter should be listening on
  • The HttpInvokerProxyFactoryBean must have the backing bean injected

Also, I would strongly advise not invoking the bean via HTTP from the same application that is exporting it. The reason for this is simply that it will mess up dependency injection, i.e. the remoteContactService will be injected into the contactExporter. Within the application, the roundtrip via HTTP is useless as you can just invoke the bean directly.

@Configuration
@Import(JdbcConfiguration.class)
@EnableWebMvc
public class HttpInvokerConfig {

    @Bean("/remoting/ContactService")
    public HttpInvokerServiceExporter contactExporter(ContactDao dao){
        HttpInvokerServiceExporter contactExporter = new HttpInvokerServiceExporter();
        contactExporter.setServiceInterface(ContactDao.class);
        contactExporter.setService(dao);
        return contactExporter;
    }
}
philippn
  • 1,850
  • 1
  • 14
  • 11