首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring HTTP调用:构建服务器上的测试崩溃

Spring HTTP调用:构建服务器上的测试崩溃
EN

Stack Overflow用户
提问于 2017-03-16 21:44:26
回答 1查看 241关注 0票数 0

我通过Spring HTTP调用构建一个API。我的config.xml看起来像这样:

代码语言:javascript
复制
<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它。

代码语言:javascript
复制
  @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,也可以从客户端应用程序调用。

但是我们的构建服务器一直说它不工作:

代码语言:javascript
复制
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)

有什么建议可以解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-20 15:56:17

经过几天乏味的bug查找后,我们找到了一个解决方案:我们的构建服务器以另一种顺序加载文件,而不是本地maven,因此我们永远不能在本地重现bug。

我们的配置文件包括以下内容:

代码语言:javascript
复制
<mvc:default-servlet-handler/>

我们的构建服务器首先加载此配置文件,然后加载api-config-file。

解决方案是让我们的SimpleUrlHandler首先注册,给它一个非常低的顺序:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42835834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档