{"id":13,"date":"2021-01-11T17:27:16","date_gmt":"2021-01-11T09:27:16","guid":{"rendered":"http:\/\/mengstudy.com\/?p=13"},"modified":"2021-01-11T19:27:43","modified_gmt":"2021-01-11T11:27:43","slug":"java%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8mockjs%e7%94%9f%e6%88%90%e6%b5%8b%e8%af%95%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=13","title":{"rendered":"Java\u9879\u76ee\u4e2d\u5982\u4f55\u4f7f\u7528MockJS\u751f\u6210\u6d4b\u8bd5\u6570\u636e"},"content":{"rendered":"\n<h2>Java\u9879\u76ee\u4e2d\u5982\u4f55\u4f7f\u7528MockJS\u751f\u6210\u6d4b\u8bd5\u6570\u636e<\/h2>\n\n\n\n<p>\u6700\u8fd1\u9879\u76ee\u4e2d\u9700\u8981\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\uff0c\u5176\u5b9e\u5728JS\u524d\u7aef\u9879\u76ee\u4e2d\u5df2\u7ecf\u6709\u6bd4\u8f83\u597d\u7684\u5f00\u6e90\u5de5\u5177mockjs\u4f5c\u4e3a\u751f\u6210\u6d4b\u8bd5\u6570\u636e\u7684\u5de5\u5177\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u62ff\u6765\u4f7f\u7528\uff0cJava\u81ea\u5e26\u6709\u811a\u672c\u6267\u884c\u5f15\u64ce\uff0cmockjs\u4f5c\u4e3a\u6807\u51c6js\u5e93\uff0c\u6ca1\u6709\u4f9d\u8d56\u4ec0\u4e48\u5176\u4ed6\u7684\u9879\u76ee\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u811a\u672c\u5f15\u64ce\u6765\u6267\u884c\u3002<\/p>\n\n\n\n<p>mockjs\u5730\u5740\uff1ahttp:\/\/mockjs.com\/<\/p>\n\n\n\n<p>\u4ecegithub\u4e0a\u53bb\u4e0b\u8f7d\u5373\u53ef<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u5730\u5740\uff1ahttps:\/\/raw.githubusercontent.com\/nuysoft\/Mock\/refactoring\/dist\/mock-min.js<\/p>\n\n\n\n<p>\u4e0b\u8f7dmockjs\u5e76\u653e\u5230\u5de5\u7a0b\u7684src\/main\/resources\/js\/\u4e0b\u9762\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u7f16\u5199\u5de5\u5177\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Slf4j\n@NoArgsConstructor(access = AccessLevel.PRIVATE)\npublic class MockJsUtils {\n    \/**\n     * Javascript\u6267\u884c\u5f15\u64ce\n     *\/\n    public static final ScriptEngine MOCK_JS_ENGINE;\n    \/**\n     * mockjs\u7684\u8d44\u6e90\u8def\u5f84\n     *\/\n    private static final String MOCK_JS_PATH = \"js\/mock-min.js\";\n\n    static {\n        MOCK_JS_ENGINE = new ScriptEngineManager().getEngineByName(\"js\");\n        try (\n                InputStream mockJs = MockJsUtils.class.getClassLoader().getResourceAsStream(MOCK_JS_PATH);\n                InputStreamReader reader = new InputStreamReader(mockJs)\n        ) {\n            MOCK_JS_ENGINE.eval(reader);\n        } catch (ScriptException | IOException e) {\n            log.error(\"\u6267\u884cMockJs\u9519\u8bef\", e);\n        }\n    }\n    public static String mock(String template) {\n        String result = StringUtils.trimToEmpty(template);\n        if (isJson(result)) {\n            try {\n                result = MOCK_JS_ENGINE.eval(\"JSON.stringify(Mock.mock(\" + result + \"))\").toString();\n            } catch (ScriptException e) {\n                log.error(\"\u6267\u884cMock.mock\u9519\u8bef\", e);\n            }\n        }\n        return result;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7b80\u5355\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Slf4j\npublic class MockJsUtilsTest {\n\n    @Test\n    public void test() {\n        String input = \"{\\n\" +\n                \"    \\\"user|2\\\": [{\\n\" +\n                \"        'name': '@cname', \\n\" +\n                \"        'id|+1': 1\\n\" +\n                \"    }]\\n\" +\n                \"}\";\n        String output = MockJsUtils.mock(input);\n        log.info(\"output: {}\", output);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"user|2\": [{\n        'name': '@cname', \n        'id|+1': 1\n    }]\n}<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\"user\":[{\"name\":\"\u66f9\u9759\",\"id\":1},{\"name\":\"\u8463\u52c7\",\"id\":2}]}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java\u9879\u76ee\u4e2d\u5982\u4f55\u4f7f\u7528MockJS\u751f\u6210\u6d4b\u8bd5\u6570\u636e \u6700\u8fd1\u9879\u76ee\u4e2d\u9700\u8981\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\uff0c\u5176\u5b9e\u5728JS\u524d\u7aef\u9879\u76ee\u4e2d\u5df2\u7ecf\u6709\u6bd4\u8f83\u597d\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,2],"tags":[],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/13"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":2,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":15,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions\/15"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}