简体   繁体   English

在Java中像在SoapUI中那样,通过代码执行结果将字符串内的常规代码替换为代码执行结果

[英]Replace pieces of groovy code inside string by code execution result with java like in SoapUI

I have xml data in config file I want to post and I need some dynamic part in this xml, like UUID, current date... 我要发布的配置文件中有xml数据,并且此xml中需要一些动态部分,例如UUID,当前日期...

In SoapUI I can use Groovy script directly inside the xml like this: 在SoapUI中,我可以像这样直接在xml内部使用Groovy脚本:

<?xml version='1.0' encoding='utf-8'?>
<Envelope>
    <Body>
        <date>${=new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX\).format(new Date())}</date>
        <uddi>${=java.util.UUID.randomUUID()}</uddi>
    <Body>
<Envelope>

And I get this 我明白了

<?xml version='1.0' encoding='utf-8'?>
<Envelope>
    <Body>
        <date>2016-04-07T15:29:43.729+02:00</date>
        <uddi>5f8020d3-1fe0-4755-8056-cdef536c98b9</uddi>
    <Body>
<Envelope>

I tryed with this code and it work well for UUID but not for SimpleDateFormat. 我尝试使用此代码,它适用于UUID,但不适用于SimpleDateFormat。

String sExecUUID = "\"UUID\" -> ${java.util.UUID.randomUUID()} !!!"; // OK : "UUID" -> 5f8020d3-1fe0-4755-8056-cdef536c98b9 !!!
String sExecDate = "\"Date\" -> ${new java.text.SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ssXXX\").format(new Date())} !!!"; // KO

//String sExec = sExecUUID;
String sExec = sExecDate;
sExec = sExec.replace("\\","\\\\"); // Replace \ --> \\
sExec = sExec.replace("\n","\\n");  // Replace \n --> \\n
sExec = sExec.replace("\r","\\r");  // Replace \n --> \\n
sExec = sExec.replace("\"","\\\""); // Replace " --> \"
sExec = "println \""+sExec+"\"";

// call groovy expressions from Java code
GroovyShell shell = new GroovyShell();
Object value = shell.evaluate(sExec);
System.out.println(value);

The exception for the date is: 日期的例外是:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 1: unexpected char: '\\' @ line 1, column 55. ew java.text.SimpleDateFormat(\\"yyyy-MM- org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:Script1.groovy:1:意外的char:'\\'@第1行,第55列。ew java.text.SimpleDateFormat(\\“ yyyy-MM-

I know it's due to my \\" in SimpleDateFormat instead of " but I have to escape " to use println . 我知道这是由于我的\\"是SimpleDateFormat而不是"但是我必须转义"才能使用println

How can I have this to work? 我该如何工作?

Groovy has an interface called TemplateEngine with differents implementations like the SimpleTemplateEngine . Groovy有一个名为TemplateEngine的接口,具有诸如SimpleTemplateEngine类的不同实现。

This template is what you need : they use a text with placeholders (${..} or the jsp syntax <%..%>). 您需要此模板:他们使用带占位符的文本($ {..}或jsp语法<%..%>)。 The placeholder can contain any groovy code. 占位符可以包含任何常规代码。

From the official documentation : 从官方文档中:

def binding = [
     firstname : "Grace",
     lastname  : "Hopper",
     accepted  : true,
     title     : 'Groovy for COBOL programmers'
 ]
 def engine = new groovy.text.SimpleTemplateEngine()
 def text = '''\
 Dear <%= firstname %> $lastname,

 We <% if (accepted) print 'are pleased' else print 'regret' %> \
 to inform you that your paper entitled
 '$title' was ${ accepted ? 'accepted' : 'rejected' }.

 The conference committee.
 '''
 def template = engine.createTemplate(text).make(binding)
 println template.toString()

see SimpleTemplateEngine 参见SimpleTemplateEngine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM