如何在 MySQL 数据库中保留 SOAP 消息 - 使用 Axis 客户端

发布于 2024-12-17 13:19:04 字数 2067 浏览 1 评论 0 原文

我正在编写一个java轴客户端,如何将原始xml保存到数据库中,到目前为止我已经找到了两种记录原始xml的方法,但它们用于控制台或文件,但我需要将每个请求和响应保存到mysql 数据库,我可以在其中使用连接作为用户参数。

这就是我已经做的事情。

  1. 将原始 ws xml 记录到console
  2. 将原始 ws xml 记录到file

好吧,我找到了一个解决方案,首先我们需要使用我之前提到的自定义处理程序(1),我们可以在消息上下文中设置属性 就像

public class FedexWsHandler extends GenericHandler {

    public QName[] getHeaders() {
        return null;
    }

    public boolean handleRequest(MessageContext context) {
    try {
        SOAPMessageContext smc = (SOAPMessageContext) context;
        SOAPMessage reqMsg = smc.getMessage();
        context.setProperty("req-msg", reqMsg);
        } catch (Exception ex) {
        ex.printStackTrace();
            }
    return true;
    }

    public boolean handleResponse(MessageContext context) {
    try {
        SOAPMessageContext smc = (SOAPMessageContext) context;
        SOAPMessage reqMsg = smc.getMessage();
        context.setProperty("res-msg", reqMsg);
        } catch (Exception ex) {
        ex.printStackTrace();
            }
    return true;
    }

}

然后在客户端中我们可以获取该属性并执行您想做的任何事情,例如

MyServiceLocator locator = new MyServiceLocator(); 
MyService service = locator.getMyService(); 
service.getResults("foo", "bar"); // call the service

// I want to get that message I have set in request and response handler methods
MessageContext ctx = locator.getCall().getMessageContext();
SOAPMessage reqMsg = (SOAPMessage) requestContext.getProperty("req-msg");         
SOAPMessage resMsg = (SOAPMessage) requestContext.getProperty("res-msg");         

但这不是一种安全的方法,因为这不是线程安全。根据其 docs< /a>

....所以如果有人可以建议我一些更好的解决方案。

I am writing a java axis client, how could I persist raw xml into data base, till now I have found two ways of logging raw xml, but they are for console or to a file, but I need to persist each request and response into mysql database, where I could use connection as a user parameter.

here is what I have done already.

  1. log raw ws xml to console
  2. log raw ws xml to a file

Well I have found a solution, First we need to use the Custom handler as I mentioned earlier(1), we can set property in the message context
like

public class FedexWsHandler extends GenericHandler {

    public QName[] getHeaders() {
        return null;
    }

    public boolean handleRequest(MessageContext context) {
    try {
        SOAPMessageContext smc = (SOAPMessageContext) context;
        SOAPMessage reqMsg = smc.getMessage();
        context.setProperty("req-msg", reqMsg);
        } catch (Exception ex) {
        ex.printStackTrace();
            }
    return true;
    }

    public boolean handleResponse(MessageContext context) {
    try {
        SOAPMessageContext smc = (SOAPMessageContext) context;
        SOAPMessage reqMsg = smc.getMessage();
        context.setProperty("res-msg", reqMsg);
        } catch (Exception ex) {
        ex.printStackTrace();
            }
    return true;
    }

}

and then in out client we can get that property and do what ever you want to do, like

MyServiceLocator locator = new MyServiceLocator(); 
MyService service = locator.getMyService(); 
service.getResults("foo", "bar"); // call the service

// I want to get that message I have set in request and response handler methods
MessageContext ctx = locator.getCall().getMessageContext();
SOAPMessage reqMsg = (SOAPMessage) requestContext.getProperty("req-msg");         
SOAPMessage resMsg = (SOAPMessage) requestContext.getProperty("res-msg");         

But it is not a safe way to do that as this is not Thread Safe. as per its docs

.... so if any one can suggest me some better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2024-12-24 13:19:04

JAXB Marshaller:StringWriter 输出具有截断的标签值

请看一下上面的帖子,您可以将 xml 转换为 java 字符串,并且您可以使用任何可以将 xml 转换为字符串的 mysql 数据类型。如果字符串非常大,则可以使用 blob。

JAXB Marshaller : StringWriter output has a truncated tag value

Please have a look on the above post, you can convert the xml to java string and and than you can use any mysql data type which can hold your xml converted to string. you can use blob if the string in very large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文