2017-03-16 71 views
0

我通过Spring HTTP Invocation构建了一个API。我的config.xml文件看起来是这样的:Spring HTTP Invocation:构建服务器上的测试崩溃

<bean id="apiUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="/remoteapi/boat">boatEndpointExporter</prop> 
     </props> 
    </property> 
</bean> 

<bean id="boatEndpointExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> 
    <property name="service" ref="rApiBoatEndpointImpl"/> 
    <property name="serviceInterface" value="xxx.RApiBoatEndpoint"/> 
</bean> 

这是我的测试案例: 它假装从应用程序外部调用API,并改掉来ping它。

@Test 
public void invokePing() { 
    RApiBoatEndpoint remoteInteface = buildRemoteInteface(); 
    assertEquals("test", remoteInteface.ping("test")); 
} 

private RApiBoatEndpoint buildRemoteInteface() { 

    String url = WebDriverResourceManager.BASE_URL + "/remoteapi/boat"; 
    System.out.println("build remote interface for RApiBoatEndpoint - url=" + url); 

    PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); 
    poolingHttpClientConnectionManager.setMaxTotal(100); 
    poolingHttpClientConnectionManager.setDefaultMaxPerRoute(5); 

    CloseableHttpClient httpClient = HttpClientBuilder.create() 
      .setConnectionManager(poolingHttpClientConnectionManager) 
      .build(); 

    HttpComponentsHttpInvokerRequestExecutor httpComponentsHttpInvokerRequestExecutor = new HttpComponentsHttpInvokerRequestExecutor(); 
    httpComponentsHttpInvokerRequestExecutor.setHttpClient(httpClient); 

    HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean(); 
    factory.setServiceUrl(url); 
    factory.setServiceInterface(RApiBoatEndpoint.class); 
    factory.setHttpInvokerRequestExecutor(httpComponentsHttpInvokerRequestExecutor); 
    factory.afterPropertiesSet(); 
    return (RApiBoatEndpoint) factory.getObject(); 
} 

该测试通过我的IDE和maven本地。 我可以手动调用API并从客户端应用程序。

但是,我们构建服务器口口声声说这是行不通的:

org.springframework.remoting.RemoteAccessException: Could not access HTTP invoker remote service at [http://xxxx/remoteapi/boat]; nested exception is org.apache.http.NoHttpResponseException: Did not receive successful HTTP response: status code = 404, status message = [Not Found] 
at org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor.validateResponse(HttpComponentsHttpInvokerRequestExecutor.java:233) 
at org.springframework.remoting.httpinvoker.HttpComponentsHttpInvokerRequestExecutor.doExecuteRequest(HttpComponentsHttpInvokerRequestExecutor.java:149) 
at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.executeRequest(AbstractHttpInvokerRequestExecutor.java:138) 
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) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
at com.sun.proxy.$Proxy36.ping(Unknown Source) 

任何建议如何解决这一问题?

+0

我觉得是这样的话wher'xxxx'在你的例外是真正重要的。那是'localhost'吗?如果不是,那么在浏览器中打开该URL时会发生什么?它真的是HTTP 404吗?测试运行时,该URL上是否有任何服务器? – SergGr

+0

@SergGr xxx是相应的buildnode的名称,如下所示:http://hudsonjava05.xxx.org:8418/xxxxx/remoteapi/boat。它在每个构建节点的context.xml中进行配置。测试通过maven运行,启动货物。还有一些其他测试(在相同的软件包中),这些测试也需要运行服务器,这些测试通过。所以我认为这不是一般问题,而是关于我的测试设置/配置。 – Shareil

+0

通常我不能调用url,因为这个服务器只运行(硒)测试。但是我会在下次建立服务器时尝试调用它......也许我会发现一些启发。 – Shareil

回答

0

经过几天的繁琐错误寻找,我们找到了一个解决方案: 我们的构建服务器以不同于本地maven的顺序加载文件,因此我们无法在本地重现此bug。

我们的配置文件中包含的:

<mvc:default-servlet-handler/> 

我们第一次加载此配置文件中构建服务器,后来的API-配置文件中。

的解决办法是让我们的SimpleUrlHandler可以给它一个非常低的顺序为先注册:

<bean id="apiUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="order" value="-1" /> 
    <property name="mappings"> 
     <props> 
      <prop key="/remoteapi/boat">boatEndpointExporter</prop> 
     </props> 
    </property> 
</bean>