1

I am trying to make gwt-2.7 work with spring-4.2.3.Configurations are:

web.xml

<!-- spring config -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring GWT integration -->
<servlet>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <url-pattern>/idp_web/service/*</url-pattern>
</servlet-mapping>

applicationContext.xml

<beans 
...
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
....
default-lazy-init="true">

<!-- auto-inject bean by annotation mechanism -->
<context:component-scan
    base-package="com.vsi.idp.analysis.server,
                    com.vsi.idp.base.server,
                    com.vsi.idp.kpi.server,
                    com.vsi.idp.map.server,//SeniorQueryServiceImpl is under this package
                    com.vsi.idp.statistics.server" />

     //other configurations
</beans>

GWT services

@RemoteServiceRelativePath("service/querySenior")
public interface SeniorQueryService extends RemoteService{...}

service impl

@Service("querySenior")
public class SeniorQueryServiceImpl extends RemoteServiceServlet implements SeniorQueryService{...}

Spock unit test works fine

@ContextConfiguration(locations = "file:war/WEB-INF/applicationContext.xml")
public class SeniorQueryServiceImplTest extends Specification{

  @Autowired
  SeniorQueryServiceImpl service

  def "query by full address"(){
      //blabla
  }
}

Running gwt project tells:

Failed to load resource: the server responded with a status of 500 (Server Error)

Error stack looks like:

[WARN] Exception while dispatching incoming RPC call
java.lang.IllegalArgumentException: Spring bean not found: querySenior
at org.spring4gwt.server.SpringGwtRemoteServiceServlet.getBean(SpringGwtRemoteServiceServlet.java:96)
at org.spring4gwt.server.SpringGwtRemoteServiceServlet.getBean(SpringGwtRemoteServiceServlet.java:55)
at org.spring4gwt.server.SpringGwtRemoteServiceServlet.processCall(SpringGwtRemoteServiceServlet.java:31)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:373)

I think:

1,"500(server error)" tells that gwt has recognized spring service
2,spring service unit test works fine,so spring configuration is right 

The problem may come from spring4gwt,and how to solve this problem?

Alex Luya
  • 9,412
  • 15
  • 59
  • 91

1 Answers1

-1

This really should be a working solution.

SpringGwtRemoteServiceServlet#getBean

protected Object getBean(String name) {
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    if (applicationContext == null) {
        throw new IllegalStateException("No Spring web application context found");
    }
    if (!applicationContext.containsBean(name)) {
        throw new IllegalArgumentException("Spring bean not found: " + name);
    }
    return applicationContext.getBean(name);
}

We can see that Exception comes because there was no bean in applicationContext

Try to implicitly declare this bean in applicatinContext.


Recomendation

If you are a fun on RPC I recommend you to take a look at GWTP and their GWT dispatch module. Approach is similar to Spring4Gwt, but it is much better to communicate with Command pattern.

With regular GWT RPC approach services in big projects becomes a mess of really lot's of methods at one place, and you will be not happy to create a new Async pair for any new method.

Or the best approach will be to communicate with JSON and avoid GWT serialization approach, your will be happy and easy to integrate with you App later.

Dmitriy Kuzkin
  • 459
  • 4
  • 4
  • Thanks,as I said,same applicationContext.xml works in spock unit test,and I used " – Alex Luya Nov 24 '15 at 01:21
  • I have created a hello world app, there is a small change in mentioned servlet(move out logging, and don't force to extend RemoteServlet) this can be seen at [github](https://github.com/kuzkdmy/spring4gwt). Looking into all details that you have provided application should be working. If you are able please share your code. Thanks. – Dmitriy Kuzkin Nov 24 '15 at 08:18