java 使用js 引擎处理业务逻辑

一个比较简单的demo,就是基于java 内置的js 引擎,扩展业务逻辑代码,实现一个xml 解析的

项目结构

  • 代码简单说明
    就是js 中使用了jackson xml 处理,同时获取xml 数组的第一个,转换为book 对象,方便业务处理
  • pom.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<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.dalong</groupId>
    <artifactId>xmljs</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.10.1</version>
        </dependency>
    </dependencies>
</project>
 
  • 代码
    很简单,就是js 引擎,使用jackson xml 的处理方法,实现一个互调用
 
package com.dalong;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class Application {
    public static void main(String[] args) throws ScriptException {
        String xmlcontent2 ="<bookstore>\n" +
                "<book category=\"COOKING\">\n" +
                "  <title lang=\"en\">Everyday Italian</title> \n" +
                "  <author>Giada De Laurentiis</author> \n" +
                "  <year>2005</year> \n" +
                "  <price>30.00</price> \n" +
                "</book>\n" +
                "<book category=\"CHILDREN\">\n" +
                "  <title lang=\"en\">Harry Potter</title> \n" +
                "  <author>J K. Rowling</author> \n" +
                "  <year>2005</year> \n" +
                "  <price>29.99</price> \n" +
                "</book>\n" +
                "<book category=\"WEB\">\n" +
                "  <title lang=\"en\">Learning XML</title> \n" +
                "  <author>Erik T. Ray</author> \n" +
                "  <year>2003</year> \n" +
                "  <price>39.95</price> \n" +
                "</book>\n" +
                "</bookstore>";
        // read xml array and then convert to list
         // 返回的实体,强制转换为 Book 对象
        String jsfunc2 = "function demo(xmlcontent) {\n" +
                "\tvar XmlMapper =  Java.type(\"com.fasterxml.jackson.dataformat.xml.XmlMapper\")\n" +
                "\tvar  myxmlmapper =new XmlMapper();\n" +
                "\tvar List =  Java.type(\"java.util.List\")\n" +
                "\tvar Book =  Java.type(\"com.dalong.Book\")\n" +
                "\tvar result = myxmlmapper.readValue(xmlcontent,List.class)\n" +
                "\treturn new Book(result[0].category,result[0].author,result[0].year,result[0].price)\n" +
                "}; demo(xml)";
        Book result = printXmlWithJs(jsfunc2,xmlcontent2);
        System.out.println(result.execute());
    }
    // result with jsonnode for better query ,also can convert to map
    public  static Book printXmlWithJs(String jsScript, String xmlContent) throws ScriptException {
        NashornScriptEngineFactory engine = new NashornScriptEngineFactory();
        SimpleBindings simpleBindings =new SimpleBindings();
        simpleBindings.putIfAbsent("xml",xmlContent);
        Object result =  engine.getScriptEngine("--language=es6").eval(jsScript,simpleBindings);
        if (result instanceof  Book){
            return (Book)result;
        }
        else {
            return  null;
        }
    }
}
 

Book.java


package com.dalong;
public class Book {
    private String category;
    public String getCategory() {
        return category;
    }
    public  Book(){
    }
    @Override
    public String toString() {
        return "Book{" +
                "category='" + category + '\'' +
                ", author='" + author + '\'' +
                ", year=" + year +
                ", price=" + price +
                '}';
    }
    public String execute(){
        return String.format("%s",this.toString());
    }
    public  Book(String  category,String author, int year,double price){
         this.author=author;
         this.category=category;
         this.year=year;
         this.price=price;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    private String author;
    private int year;
    private double price;
}
 
  • 运行效果

 

 

说明

实际如果需要使用我们可以集成spi 或者jar 包模式的进行扩展

参考资料

https://github.com/FasterXML/jackson-dataformat-xml

posted on 2020-10-27 19:41  荣锋亮  阅读(1310)  评论(0编辑  收藏  举报

导航