1. 程式人生 > 實用技巧 >java 使用js 引擎處理業務邏輯

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