|
| 1 | +/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon/APIJSON) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package apijson.demo; |
| 16 | + |
| 17 | +import apijson.gson.*; |
| 18 | +import org.springframework.boot.SpringApplication; |
| 19 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 20 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 21 | +import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
| 22 | +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.web.servlet.config.annotation.CorsRegistry; |
| 26 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 27 | + |
| 28 | +import apijson.Log; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.LinkedHashMap; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Demo SpringBoot Application 主应用程序启动类 |
| 37 | + * 右键这个类 > Run As > Java Application |
| 38 | + * 具体见 SpringBoot 文档 |
| 39 | + * https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class |
| 40 | + * |
| 41 | + * @author Lemon |
| 42 | + */ |
| 43 | +@Configuration |
| 44 | +@SpringBootApplication |
| 45 | +@EnableConfigurationProperties |
| 46 | +public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + SpringApplication.run(DemoApplication.class, args); |
| 50 | + |
| 51 | + Log.DEBUG = true; |
| 52 | + |
| 53 | + // 使用本项目的自定义处理类 |
| 54 | + APIJSONCreator<Long> creator = new APIJSONCreator<Long>() { |
| 55 | + |
| 56 | + @Override |
| 57 | + public DemoParser createParser() { |
| 58 | + return new DemoParser(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public DemoFunctionParser createFunctionParser() { |
| 63 | + return new DemoFunctionParser(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public DemoVerifier createVerifier() { |
| 68 | + return new DemoVerifier(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public DemoSQLConfig createSQLConfig() { |
| 73 | + return new DemoSQLConfig(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public DemoSQLExecutor createSQLExecutor() { |
| 78 | + return new DemoSQLExecutor(); |
| 79 | + } |
| 80 | + |
| 81 | + }; |
| 82 | + |
| 83 | + APIJSONApplication.init(false, creator); // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效 |
| 84 | + } |
| 85 | + |
| 86 | + // SpringBoot 2.x 自定义端口方式 |
| 87 | + @Override |
| 88 | + public void customize(ConfigurableServletWebServerFactory server) { |
| 89 | + server.setPort(8080); |
| 90 | + } |
| 91 | + |
| 92 | + // 支持 APIAuto 中 JavaScript 代码跨域请求 |
| 93 | + @Bean |
| 94 | + public WebMvcConfigurer corsConfigurer() { |
| 95 | + return new WebMvcConfigurer() { |
| 96 | + @Override |
| 97 | + public void addCorsMappings(CorsRegistry registry) { |
| 98 | + registry.addMapping("/**") |
| 99 | + .allowedOriginPatterns("*") |
| 100 | + .allowedMethods("*") |
| 101 | + .allowCredentials(true) |
| 102 | + .maxAge(3600); |
| 103 | + } |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + static { |
| 108 | + //// 使用 gjson |
| 109 | + //apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() { |
| 110 | + // |
| 111 | + // @Override |
| 112 | + // public JSONObject createJSONObject() { |
| 113 | + // return new JSONObject(true); |
| 114 | + // } |
| 115 | + // |
| 116 | + // @Override |
| 117 | + // public JSONArray createJSONArray() { |
| 118 | + // return new JSONArray(); |
| 119 | + // } |
| 120 | + // |
| 121 | + // @Override |
| 122 | + // public String toJSONString(Object obj, boolean format) { |
| 123 | + // return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj); |
| 124 | + // } |
| 125 | + // |
| 126 | + // @Override |
| 127 | + // public Object parseJSON(Object json) { |
| 128 | + // return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES); |
| 129 | + // } |
| 130 | + // |
| 131 | + // @Override |
| 132 | + // public JSONObject parseObject(Object json) { |
| 133 | + // return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES); |
| 134 | + // } |
| 135 | + // |
| 136 | + // @Override |
| 137 | + // public <T> T parseObject(Object json, Class<T> clazz) { |
| 138 | + // return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES); |
| 139 | + // } |
| 140 | + // |
| 141 | + // @Override |
| 142 | + // public JSONArray parseArray(Object json) { |
| 143 | + // return JSON.parseArray(toJSONString(json)); |
| 144 | + // } |
| 145 | + // |
| 146 | + // @Override |
| 147 | + // public <T> List<T> parseArray(Object json, Class<T> clazz) { |
| 148 | + // return JSON.parseArray(toJSONString(json), clazz); |
| 149 | + // } |
| 150 | + // |
| 151 | + //}; |
| 152 | + |
| 153 | + |
| 154 | + // 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增 |
| 155 | + // try { //加载驱动程序 |
| 156 | + // Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 157 | + // Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); |
| 158 | + // Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 159 | + // } |
| 160 | + // catch (ClassNotFoundException e) { |
| 161 | + // e.printStackTrace(); |
| 162 | + // Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!"); |
| 163 | + // } |
| 164 | + // |
| 165 | + // try { //加载驱动程序 |
| 166 | + // Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< "); |
| 167 | + // Class.forName("oracle.jdbc.driver.OracleDriver"); |
| 168 | + // Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> "); |
| 169 | + // } |
| 170 | + // catch (ClassNotFoundException e) { |
| 171 | + // e.printStackTrace(); |
| 172 | + // Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!"); |
| 173 | + // } |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments