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   荣锋亮  阅读(1343)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-10-27 dolt 基于git协议的数据管理工具
2019-10-27 docz 强大简单的文档管理工具
2019-10-27 yugabyte 安装pg extention
2018-10-27 hasura graphql-engine v1.0.0-alpha26 版本新功能试用
2018-10-27 hasura graphql-engine v1.0.0-alpha26 版本新功能

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示