|  | 
|  | 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.jackson.*; | 
|  | 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 | + | 
|  | 31 | +/** | 
|  | 32 | + * Demo SpringBoot Application 主应用程序启动类 | 
|  | 33 | + * 右键这个类 > Run As > Java Application | 
|  | 34 | + * 具体见 SpringBoot 文档 | 
|  | 35 | + * https://www.springcloud.cc/spring-boot.html#using-boot-locating-the-main-class | 
|  | 36 | + * | 
|  | 37 | + * @author Lemon | 
|  | 38 | + */ | 
|  | 39 | +@Configuration | 
|  | 40 | +@SpringBootApplication | 
|  | 41 | +@EnableConfigurationProperties | 
|  | 42 | +public class DemoApplication implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { | 
|  | 43 | + | 
|  | 44 | +  public static void main(String[] args) throws Exception { | 
|  | 45 | +    SpringApplication.run(DemoApplication.class, args); | 
|  | 46 | + | 
|  | 47 | +    Log.DEBUG = true; | 
|  | 48 | + | 
|  | 49 | +    // 使用本项目的自定义处理类 | 
|  | 50 | +    APIJSONCreator<Long> creator = new APIJSONCreator<Long>() { | 
|  | 51 | + | 
|  | 52 | +      @Override | 
|  | 53 | +      public DemoParser createParser() { | 
|  | 54 | +        return new DemoParser(); | 
|  | 55 | +      } | 
|  | 56 | + | 
|  | 57 | +      @Override | 
|  | 58 | +      public DemoFunctionParser createFunctionParser() { | 
|  | 59 | +        return new DemoFunctionParser(); | 
|  | 60 | +      } | 
|  | 61 | + | 
|  | 62 | +      @Override | 
|  | 63 | +      public DemoVerifier createVerifier() { | 
|  | 64 | +        return new DemoVerifier(); | 
|  | 65 | +      } | 
|  | 66 | + | 
|  | 67 | +      @Override | 
|  | 68 | +      public DemoSQLConfig createSQLConfig() { | 
|  | 69 | +        return new DemoSQLConfig(); | 
|  | 70 | +      } | 
|  | 71 | + | 
|  | 72 | +      @Override | 
|  | 73 | +      public DemoSQLExecutor createSQLExecutor() { | 
|  | 74 | +        return new DemoSQLExecutor(); | 
|  | 75 | +      } | 
|  | 76 | + | 
|  | 77 | +    }; | 
|  | 78 | + | 
|  | 79 | +    APIJSONApplication.init(false, creator);  // 4.4.0 以上需要这句来保证以上 static 代码块中给 DEFAULT_APIJSON_CREATOR 赋值会生效 | 
|  | 80 | +  } | 
|  | 81 | + | 
|  | 82 | +  // SpringBoot 2.x 自定义端口方式 | 
|  | 83 | +  @Override | 
|  | 84 | +  public void customize(ConfigurableServletWebServerFactory server) { | 
|  | 85 | +    server.setPort(8080); | 
|  | 86 | +  } | 
|  | 87 | + | 
|  | 88 | +  // 支持 APIAuto 中 JavaScript 代码跨域请求 | 
|  | 89 | +  @Bean | 
|  | 90 | +  public WebMvcConfigurer corsConfigurer() { | 
|  | 91 | +    return new WebMvcConfigurer() { | 
|  | 92 | +      @Override | 
|  | 93 | +      public void addCorsMappings(CorsRegistry registry) { | 
|  | 94 | +        registry.addMapping("/**") | 
|  | 95 | +          .allowedOriginPatterns("*") | 
|  | 96 | +          .allowedMethods("*") | 
|  | 97 | +          .allowCredentials(true) | 
|  | 98 | +          .maxAge(3600); | 
|  | 99 | +      } | 
|  | 100 | +    }; | 
|  | 101 | +  } | 
|  | 102 | + | 
|  | 103 | +  static { | 
|  | 104 | +    //// 使用 jackson | 
|  | 105 | +    //apijson.JSON.DEFAULT_JSON_PARSER = new JSONParser<JSONObject, JSONArray>() { | 
|  | 106 | +    // | 
|  | 107 | +    //  @Override | 
|  | 108 | +    //  public JSONObject createJSONObject() { | 
|  | 109 | +    //    return new JSONObject(true); | 
|  | 110 | +    //  } | 
|  | 111 | +    // | 
|  | 112 | +    //  @Override | 
|  | 113 | +    //  public JSONArray createJSONArray() { | 
|  | 114 | +    //    return new JSONArray(); | 
|  | 115 | +    //  } | 
|  | 116 | +    // | 
|  | 117 | +    //  @Override | 
|  | 118 | +    //  public String toJSONString(Object obj, boolean format) { | 
|  | 119 | +    //    return obj == null || obj instanceof String ? (String) obj : JSON.toJSONString(obj); | 
|  | 120 | +    //  } | 
|  | 121 | +    // | 
|  | 122 | +    //  @Override | 
|  | 123 | +    //  public Object parseJSON(Object json) { | 
|  | 124 | +    //    return JSON.parse(toJSONString(json), DEFAULT_FASTJSON_FEATURES); | 
|  | 125 | +    //  } | 
|  | 126 | +    // | 
|  | 127 | +    //  @Override | 
|  | 128 | +    //  public JSONObject parseObject(Object json) { | 
|  | 129 | +    //    return JSON.parseObject(toJSONString(json), DEFAULT_FASTJSON_FEATURES); | 
|  | 130 | +    //  } | 
|  | 131 | +    // | 
|  | 132 | +    //  @Override | 
|  | 133 | +    //  public <T> T parseObject(Object json, Class<T> clazz) { | 
|  | 134 | +    //    return JSON.parseObject(toJSONString(json), clazz, DEFAULT_FASTJSON_FEATURES); | 
|  | 135 | +    //  } | 
|  | 136 | +    // | 
|  | 137 | +    //  @Override | 
|  | 138 | +    //  public JSONArray parseArray(Object json) { | 
|  | 139 | +    //    return JSON.parseArray(toJSONString(json)); | 
|  | 140 | +    //  } | 
|  | 141 | +    // | 
|  | 142 | +    //  @Override | 
|  | 143 | +    //  public <T> List<T> parseArray(Object json, Class<T> clazz) { | 
|  | 144 | +    //    return JSON.parseArray(toJSONString(json), clazz); | 
|  | 145 | +    //  } | 
|  | 146 | +    // | 
|  | 147 | +    //}; | 
|  | 148 | + | 
|  | 149 | + | 
|  | 150 | +    // 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增 | 
|  | 151 | +    //		try { //加载驱动程序 | 
|  | 152 | +    //			Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< "); | 
|  | 153 | +    //			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); | 
|  | 154 | +    //			Log.d(TAG, "成功加载 SQLServer 驱动!>>>>>>>>>>>>>>>>>>>>> "); | 
|  | 155 | +    //		} | 
|  | 156 | +    //		catch (ClassNotFoundException e) { | 
|  | 157 | +    //			e.printStackTrace(); | 
|  | 158 | +    //			Log.e(TAG, "加载 SQLServer 驱动失败,请检查 pom.xml 中 net.sourceforge.jtds 版本是否存在以及可用 !!!"); | 
|  | 159 | +    //		} | 
|  | 160 | +    // | 
|  | 161 | +    //		try { //加载驱动程序 | 
|  | 162 | +    //			Log.d(TAG, "尝试加载 Oracle 驱动 <<<<<<<<<<<<<<<<<<<<< "); | 
|  | 163 | +    //			Class.forName("oracle.jdbc.driver.OracleDriver"); | 
|  | 164 | +    //			Log.d(TAG, "成功加载 Oracle 驱动!>>>>>>>>>>>>>>>>>>>>> "); | 
|  | 165 | +    //		} | 
|  | 166 | +    //		catch (ClassNotFoundException e) { | 
|  | 167 | +    //			e.printStackTrace(); | 
|  | 168 | +    //			Log.e(TAG, "加载 Oracle 驱动失败,请检查 pom.xml 中 com.oracle.jdbc 版本是否存在以及可用 !!!"); | 
|  | 169 | +    //		} | 
|  | 170 | + | 
|  | 171 | +  } | 
|  | 172 | + | 
|  | 173 | +} | 
0 commit comments