Apache CXF Simple Frontend Issue: Connection Already Established Error

I’m working with Apache CXF and trying to set up a web service using the simple frontend approach. I want to expose my Spring service as a web service without adding JAX-WS annotations to keep it clean.

My server setup:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ps="http://example.org/payment/service"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
    http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="byType">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<import resource="classpath*:applicationContext.xml" /> 

<simple:server id="paymentServer" serviceBean="paymentProcessor"
    serviceClass="com.example.PaymentService"
    bindingId="http://apache.org/cxf/binding/http"
    address="/${service.path}"
    serviceName="ps:paymentProcessor"
    endpointName="ps:paymentProcessorPort">
    <simple:dataBinding>
        <bean class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
    </simple:dataBinding>
    <simple:binding>
        <soap:soapBinding version="1.2" mtomEnabled="true" />
    </simple:binding>
</simple:server>

<context:property-placeholder location="classpath:app.properties" />
</beans>

My client config:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:simple="http://cxf.apache.org/simple"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ps="http://example.org/payment/service"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
    http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

<simple:client id="paymentProcessor" wsdlLocation="${service.url}?wsdl"
    serviceName="ps:paymentProcessor"
    endpointName="ps:paymentProcessorPort"
    transportId="http://schemas.xmlsoap.org/soap/http"
    address="${service.url}"
    bindingId="http://apache.org/cxf/binding/http"
    serviceClass="com.example.PaymentService">
    <simple:dataBinding>
        <bean class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
    </simple:dataBinding>
    <simple:binding>
        <soap:soapBinding mtomEnabled="true" version="1.2" />
    </simple:binding>
</simple:client>

<context:property-placeholder location="classpath:app.properties" />
</beans>

When I try to call any method on the service, I get this error:

java.lang.IllegalStateException: Already connected
    at java.net.HttpURLConnection.setFixedLengthStreamingMode(HttpURLConnection.java:103)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.thresholdNotReached(HTTPConduit.java:1889)
    at org.apache.cxf.io.AbstractThresholdOutputStream.close(AbstractThresholdOutputStream.java:99)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1980)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:66)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:639)

This happens right on the first service call. The WSDL loads fine but when I actually invoke a method, I get this connection error. Has anyone seen this before? I’m using the simple frontend because my service doesn’t have JAX-WS annotations and I want to keep it that way.

had this exact issue a few months back! you’re mixing http binding with soap binding, which messes up cxf’s connection handling. remove the bindingId="http://apache.org/cxf/binding/http" from both server and client configs - you’re using soap binding anyway. also double-check your service.url property has the right protocol. should be http, not https if you’re testing locally.

I encountered a similar problem when configuring Apache CXF with the Simple Frontend. The root cause is likely a conflict between your bindingId set to http://apache.org/cxf/binding/http and the <soap:soapBinding> configuration. This can lead CXF to attempt to establish connections using different protocols, causing the ‘Already connected’ error. To resolve this, try removing the bindingId attribute altogether; this allows the SOAP binding to correctly manage the protocol. Additionally, ensure that the transportId in your client matches the binding in your server configuration to avoid such connection issues.

wait, does this error happen on evry method call or just some? what cxf version r u using? i’ve seen different versions handle http/soap binding issues differently. try turning on debug logging - it’ll show u what’s going on with the connection lifecycle.