SAAJ0537:无效的内容类型。可能是错误消息而不是SOAP消息

时间:2017-10-30 22:43:58

标签: spring maven soap jboss saaj

目前我正在实现一个用例,我想创建一个通过HTTP协议写入的soap客户端。 为了测试相同的用例,我启动并运行了一个模拟服务。现在,当我执行我的SAAJ实现作为一个独立的jar时,我的应用程序运行良好,并没有任何问题。

我目前的问题是,当我在基于Spring 4的应用程序中合并该代码并将其部署在Jboss AS 7.1.1上时,它给出了如下错误:

12:40:33,992 ERROR [stderr] (ActiveMQ Session Task-1) com.sun.xml.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?

12:40:33,994 ERROR [stderr] (ActiveMQ Session Task-1)   at com.sun.xml.messaging.saaj.soap.MessageImpl.identifyContentType(MessageImpl.java:670)

12:40:33,996 ERROR [stderr] (ActiveMQ Session Task-1)   at com.sun.xml.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:100)

12:40:33,997 ERROR [stderr] (ActiveMQ Session Task-1)   at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:342)

12:40:33,999 ERROR [stderr] (ActiveMQ Session Task-1)   at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:159)

最初我认为这是一个问题,因为其他春天依赖,因为以前我一直在努力解决相同的问题,但这不是这里的情况,正如你可以在下面的pom条目中看到我删除了冲突的spring -boot - 通过使其成为可选的,从我的战争构建中自动配置APIS。

我的maven依赖关系如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.saaj.client</groupId>
    <artifactId>notification</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>notification</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.1.6.RELEASE</spring.version><!-- 3.0.5.RELEASE 4.1.6.RELEASE -->
        <activemq.version>5.14.3</activemq.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.4.3.RELEASE</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test-autoconfigure</artifactId>
            <version>1.4.3.RELEASE</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-spring</artifactId>
            <version>${activemq.version}</version>
        </dependency>
        <!-- 
        <dependency>
            <groupId>com.sun.xml.messaging.saaj</groupId>
            <artifactId>saaj-impl</artifactId>
            <version>1.3</version>
        </dependency>
 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
            <optional>false</optional>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.6</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.2.1</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>8.4-702.jdbc4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.6</version>
        </dependency>

    </dependencies>

</project>

我的SAAJ实施如下:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.xml.sax.InputSource;

import com.saaj.client.constants.NotificationConstants;
import com.saaj.client.exception.CustomException;
import com.saaj.client.logger.LoggerManager;

@Component("adapterServiceHelper")
public class AdapterServiceHelper {

    private static Logger logger = LoggerManager.getInstance().getNotificationProcessingLogger();

    public AdapterServiceHelper(){

    }

    public URL getEndpointConnection(String url) throws MalformedURLException, CustomException{
        URL endpoint = null;

        try{
            endpoint = new URL (null, url, new URLStreamHandler () {
                protected URLConnection openConnection (URL url) throws IOException {
                    // The url is the parent of this stream handler, so must create clone
                    URL clone = new URL (url.toString ());
                    URLConnection connection = clone.openConnection ();
                    connection.setRequestProperty("Content-Type",
                            "text/xml");
                    connection.setRequestProperty("Accept",
                            "application/soap+xml, text/*");
                    // If we cast to HttpURLConnection, we can set redirects
                    // connection.setInstanceFollowRedirects (false);
                    connection.setConnectTimeout (5 * 1000);      // 15 sec
                    connection.setReadTimeout (5 * 1000);         // 15 sec
                    // Custom header
                    return connection;
                }});


        }catch(MalformedURLException e){
            throw e;
        }catch(Exception exp){
            exp.printStackTrace();
            throw new CustomException(exp.getMessage(),"Error occured while getting endpoint connection to IP");
        }
        return endpoint;
    }

    //main SAAJ implementation
    public SOAPMessage writeCommand(URL endpoint, String command) throws Exception{

        SOAPMessage soapResponse = null;
        SOAPConnection soapConnection = null;
        InputStream is = null;
        System.out.println("command written is "+command);
        try{
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
                    .newInstance();
            soapConnection = soapConnectionFactory
                    .createConnection();

            //write to PG over soapConnection can be made generic
            is = new ByteArrayInputStream(command.getBytes());
            SOAPMessage request = MessageFactory.newInstance(
                    SOAPConstants.SOAP_1_1_PROTOCOL).createMessage(
                            new MimeHeaders(), is);

            MimeHeaders headers = request.getMimeHeaders();
            headers.addHeader("Content-Type", "text/xml");
            request.saveChanges();


            soapResponse = soapConnection.call(request, endpoint);
            //soapConnection.close();
            //is.close();
            //process soap response
            printSOAPResponse(soapResponse);
        }
        catch(SOAPException exp)
        {
            exp.printStackTrace();
            logger.error("ERROR while connecting SOAP service" + exp.getMessage());
            throw new Exception("ERROR : SOAPException encountered while writing ");
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("ERROR : SOAPException encountered while writing ");
        } catch (TransformerException e) {
            e.printStackTrace();
            throw new Exception("ERROR : SOAPException encountered while writing ");
        }
        finally{
            soapConnection.close();
            is.close();
        }
        return soapResponse;

    }


    public static void printSOAPResponse(SOAPMessage soapResponse) throws TransformerException, SOAPException {
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }

}

如果对此问题有任何建议,请告诉我。

3 个答案:

答案 0 :(得分:0)

我尝试使用基本的java.net HTTP连接来检查我得到的数据,发现我真的得到了模拟服务端口的html页面。(localhost:9997) 说我的服务在URL上:&#34; http://localhost:9997/mockSoapBinding&#34; 所以我收到了网址的数据:&#34; http://localhost:9997&#34;而不是暴露的Web服务。

要检查相同,我使用下面的代码:

public String handleRequest(String method, String url, String xmlPayload,String readTimeout,String connectTimeout,String soapAction) throws IOException {
    HttpURLConnection connection = null;
    OutputStreamWriter wr = null;
    BufferedReader in = null;
    String result = "";

    try {
        URL connectionUrl = new URL(url);
        connection = (HttpURLConnection) connectionUrl.openConnection();
        connection.setRequestMethod(method);
        if ( xmlPayload != null && ! xmlPayload.equals("")) {
            connection.setReadTimeout(Integer.parseInt(readTimeout));
            connection.setConnectTimeout(Integer.parseInt(connectTimeout));
            connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            /*connection.setRequestProperty("SOAPAction", soapAction);*/
            connection.setDoOutput(true);
            wr = new OutputStreamWriter(connection.getOutputStream());
            wr.write(xmlPayload);
            wr.flush();
        }
        try {
            // getInputStream will throw exception when response code is
            // greater than 400, so we need to getErrorStream
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        }
        StringBuilder bodyBuilder = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            bodyBuilder.append(inputLine);
        }
        in.close();
        result = String.valueOf( bodyBuilder.toString());
        System.out.println(result);

    } catch(Exception e){
        logger.error("Error occured while send message for : " , e);
            }finally {
        if (wr != null) {
            wr.close();
        }
        if (in != null) {
            in.close();
        }
        if ( connection != null ) {
            connection.disconnect();
        }

    }

    return result;
}

请注意,这也是SAAJ实施的替代方案。

答案 1 :(得分:0)

当我在soap enveloppe中提供了错误的名称空间时,收到了此错误消息。这可能与接受的答案描述的效果相同,即请求到达错误的URL。

答案 2 :(得分:0)

我发现了一个依赖问题。

POI 4.0.1 正常,5.0.0 失败,使用这个 pom.xml :

    </dependencies>
        <!-- @see https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception -->
        <!-- API, java.xml.bind module -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </dependency>
        
        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
<!--            <version>5.0.0</version> -->
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
<!--            <version>5.0.0</version> -->
        </dependency>
    </dependencies>