Java 使用JMS和Spring的HTTP上的XML

Java 使用JMS和Spring的HTTP上的XML,java,web-services,spring,http,jms,Java,Web Services,Spring,Http,Jms,我有一个传统的HTTP服务器,在那里我需要使用Java(而不是浏览器)通过HTTP请求(POST)发送XML文件,服务器将在其HTTP响应中使用另一个XML。它类似于Web服务,但没有WSDL,我必须遵循现有的XML结构来构造要发送的XML 我做了一项研究,发现了一个符合我要求的例子。该示例使用Apache Commons中的HttpClient。(我还发现了其他一些示例,但它们使用java.net网络包(比如URLConnection),这很乏味,所以我不想使用它们) 但我也要求使用Sprin

我有一个传统的HTTP服务器,在那里我需要使用
Java
(而不是浏览器)通过
HTTP请求
POST
)发送
XML
文件,服务器将在其
HTTP响应中使用另一个
XML
。它类似于Web服务,但没有WSDL,我必须遵循现有的XML结构来构造要发送的XML

我做了一项研究,发现了一个符合我要求的例子。该示例使用Apache Commons中的
HttpClient
。(我还发现了其他一些示例,但它们使用
java.net
网络包(比如
URLConnection
),这很乏味,所以我不想使用它们)

但我也要求使用
Spring
JMS

我从
Spring
了解到,可以将
HttpClient
JMS
Spring
组合起来。我的问题是,怎么做

请注意,我的要求是使用
HttpClient
,而不是
。如果你有更好的建议,欢迎

谢谢


以下是我一直在谈论的XML over HTTP示例,供您参考:

/*
 * $Header: 
 * $Revision$
 * $Date$
 * ====================================================================
 *
 *  Copyright 2002-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 */

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 *
 * This is a sample application that demonstrates
 * how to use the Jakarta HttpClient API.
 *
 * This application sends an XML document
 * to a remote web server using HTTP POST
 *
 * @author Sean C. Sullivan
 * @author Ortwin Glück
 * @author Oleg Kalnichevski
 */
public class PostXML {

    /**
     *
     * Usage:
     *          java PostXML http://mywebserver:80/ c:\foo.xml
     *
     *  @param args command line arguments
     *                 Argument 0 is a URL to a web server
     *                 Argument 1 is a local filename
     *
     */
    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            System.out.println(
                "Usage: java -classpath <classpath> [-Dorg.apache.commons."+
                "logging.simplelog.defaultlog=<loglevel>]" +
                " PostXML <url> <filename>]");

            System.out.println("<classpath> - must contain the "+
                "commons-httpclient.jar and commons-logging.jar");

            System.out.println("<loglevel> - one of error, "+
                    "warn, info, debug, trace");

            System.out.println("<url> - the URL to post the file to");
            System.out.println("<filename> - file to post to the URL");
            System.out.println();
            System.exit(1);
        }

        // Get target URL
        String strURL = args[0];

        // Get file to be posted
        String strXMLFilename = args[1];
        File input = new File(strXMLFilename);

        // Prepare HTTP post
        PostMethod post = new PostMethod(strURL);

        // Request content will be retrieved directly
        // from the input stream
        // Per default, the request content needs to be buffered
        // in order to determine its length.
        // Request body buffering can be avoided when
        // content length is explicitly specified
        post.setRequestEntity(new InputStreamRequestEntity(
                new FileInputStream(input), input.length()));

        // Specify content type and encoding
        // If content encoding is not explicitly specified
        // ISO-8859-1 is assumed
        post.setRequestHeader(
                "Content-type", "text/xml; charset=ISO-8859-1");

        // Get HTTP client
        HttpClient httpclient = new HttpClient();

        // Execute request
        try {

            int result = httpclient.executeMethod(post);

            // Display status code
            System.out.println("Response status code: " + result);

            // Display response
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());

        } finally {
            // Release current connection to the connection pool 
            // once you are done
            post.releaseConnection();
        }
    }
}
/*
*$Header:
*$Revision$
*$Date$
* ====================================================================
*
* Apache软件基金会版权2002年至2004年
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
* ====================================================================
*
*该软件由许多人的自愿捐款组成
*代表Apache软件基金会的个人。更多
*关于Apache软件基金会的信息,请参阅
* .
*
*[附加通知,如先前许可条件要求]
*
*/
导入java.io.File;
导入java.io.FileInputStream;
导入org.apache.commons.httpclient.httpclient;
导入org.apache.commons.httpclient.methods.InputStreamRequestEntity;
导入org.apache.commons.httpclient.methods.PostMethod;
/**
*
*这是一个示例应用程序,演示了
*如何使用雅加达HttpClient API。
*
*此应用程序发送一个XML文档
*使用HTTP POST发送到远程web服务器
*
*@作者肖恩·C·沙利文
*@author Ortwin Glück
*@作者Oleg Kalnichevski
*/
公共类PostXML{
/**
*
*用法:
*java PostXMLhttp://mywebserver:80/ c:\foo.xml
*
*@param args命令行参数
*参数0是指向web服务器的URL
*参数1是本地文件名
*
*/
公共静态void main(字符串[]args)引发异常{
如果(参数长度!=2){
System.out.println(
“用法:java-classpath[-Dorg.apache.commons。”+
“logging.simplelog.defaultlog=]”+
“PostXML]”;
System.out.println(“-必须包含”+
“commons-httpclient.jar和commons-logging.jar”);
System.out.println(“-错误之一,”+
“警告、信息、调试、跟踪”);
System.out.println(“-发布文件的URL”);
System.out.println(“-要发布到URL的文件”);
System.out.println();
系统出口(1);
}
//获取目标URL
字符串strURL=args[0];
//获取要发布的文件
字符串strXMLFilename=args[1];
文件输入=新文件(strXMLFilename);
//准备HTTP post
PostMethod post=新的PostMethod(strURL);
//将直接检索请求内容
//从输入流
//默认情况下,需要缓冲请求内容
//以确定其长度。
//在以下情况下可以避免请求主体缓冲
//内容长度是明确指定的
post.setRequestEntity(新的InputStreamRequestEntity(
新文件inputstream(input),input.length());
//指定内容类型和编码
//如果未明确指定内容编码
//假定为ISO-8859-1
post.setRequestHeader(
“内容类型”、“文本/xml;字符集=ISO-8859-1”);
//获取HTTP客户端
HttpClient HttpClient=新HttpClient();
//执行请求
试一试{
int result=httpclient.executeMethod(post);
//显示状态代码
System.out.println(“响应状态代码:+结果”);
//显示响应
System.out.println(“响应主体:”);
System.out.println(post.getResponseBodyAsString());
}最后{
//释放到连接池的当前连接
//完成后
post.releaseConnection();
}
}
}

在这种情况下不需要JMS,它是为异步消息传递而设计的,而不是请求响应


您需要的是Spring的
RestTemplate
。它与HttpClient类似,但如果您有架构,它将为您处理封送。

我说过我没有架构。我需要使用JMS(异步消息传递),因为响应可能很慢。您不能将JMS与HTTP一起使用。这两种技术不兼容,为什么不兼容?我还没有读过这篇文章,但我发现:这是HTTP上的JMS。如果现有的HTTP服务器希望为您提供一些XML,那么它就不会神奇地理解JMS。