Skip to content

Commit

Permalink
Java:所有 Demo 升级自身, APIJSON, apijson-framework 版本至 5.1.0,升级 UnitAuto 至…
Browse files Browse the repository at this point in the history
… 2.7.2
  • Loading branch information
TommyLemon committed May 3, 2022
1 parent ba1f96a commit ee020d3
Show file tree
Hide file tree
Showing 33 changed files with 295 additions and 180 deletions.
2 changes: 1 addition & 1 deletion APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>apijson.boot</groupId>
<artifactId>apijson-boot</artifactId>
<artifactId>apijson-boot-multi-datasource</artifactId>
<version>5.1.0</version>
<!-- <packaging>jar</packaging> -->

Expand Down
Expand Up @@ -228,12 +228,13 @@ public String delete(@RequestBody String request, HttpSession session) {

// 以上接口对应的简版接口,格式为 {method}/{tag}?format=true&@explain=true.. <<<<<<<<<<<<<<<<<<<<<<<<<


/**增删改查统一入口,这个一个方法可替代以下 7 个方法,牺牲一些路由解析性能来提升一点开发效率
* @param request 只用String,避免encode后未decode
/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param tag
* @param params
* @param request
* @param session
* @return
* @see {@link RequestMethod#DELETE}
*/
@PostMapping("crud/{method}/{tag}") // 直接 {method}/{tag} 或 apijson/{method}/{tag} 会和内置网页的路由有冲突
@Override
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 7 additions & 8 deletions APIJSON-Java-Server/APIJSONBoot/pom.xml
Expand Up @@ -5,7 +5,7 @@

<groupId>apijson.boot</groupId>
<artifactId>apijson-boot</artifactId>
<version>5.0.0</version>
<version>5.1.0</version>
<!-- <packaging>jar</packaging> -->

<name>APIJSONBoot</name>
Expand All @@ -32,17 +32,16 @@
<version>4.0.1</version>
</dependency>

<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一
<<<<<<<<< -->
<!-- 单元测试:可使用 libs 目录的 unitauto-java.jar 和 unitauto-jar.jar 来替代,两种方式二选一 <<<<<<<<< -->
<dependency>
<groupId>com.github.TommyLemon</groupId>
<artifactId>unitauto-java</artifactId>
<version>2.6.4</version>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.github.TommyLemon</groupId>
<artifactId>unitauto-jar</artifactId>
<version>2.6.4</version>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
Expand All @@ -56,17 +55,17 @@
<dependency>
<groupId>com.github.Tencent</groupId>
<artifactId>APIJSON</artifactId>
<version>5.0.0</version>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.github.APIJSON</groupId>
<artifactId>apijson-framework</artifactId>
<version>5.0.0</version>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.github.APIJSON</groupId>
<artifactId>apijson-column</artifactId>
<version>1.2.0</version>
<version>1.2.2</version>
</dependency>
<!-- 可使用 libs 目录的 apijson-orm.jar, apijson-framework.jar, apijson-column.jar 来替代,两种方式二选一 >>>>>>>>>> -->

Expand Down
Expand Up @@ -72,6 +72,23 @@
public class DemoApplication implements ApplicationContextAware, WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
private static final String TAG = "DemoApplication";

public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);

// FIXME 不要开放给项目组后端之外的任何人使用 UnitAuto(强制登录鉴权)!!!如果不需要单元测试则移除相关代码或 unitauto.Log.DEBUG = false;
// 上线生产环境前改为 false,可不输出 APIJSONORM 的日志 以及 SQLException 的原始(敏感)信息
unitauto.Log.DEBUG = Log.DEBUG = true;
APIJSONParser.IS_PRINT_BIG_LOG = true;
APIJSONApplication.init();
}

// SpringBoot 2.x 自定义端口方式
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(8080);
}


static {
// APIJSON 配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Expand All @@ -81,7 +98,7 @@ public class DemoApplication implements ApplicationContextAware, WebServerFactor
COMPILE_MAP.put("ID_CARD", StringUtil.PATTERN_ID_CARD);

// 使用本项目的自定义处理类
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator() {
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {

@Override
public Parser<Long> createParser() {
Expand Down Expand Up @@ -242,21 +259,6 @@ public boolean apply(Object object, String name, Object value) {
}


public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);

// FIXME 不要开放给项目组后端之外的任何人使用 UnitAuto(强制登录鉴权)!!!如果不需要单元测试则移除相关代码或 unitauto.Log.DEBUG = false;
// 上线生产环境前改为 false,可不输出 APIJSONORM 的日志 以及 SQLException 的原始(敏感)信息
unitauto.Log.DEBUG = Log.DEBUG = true;
APIJSONParser.IS_PRINT_BIG_LOG = true;
APIJSONApplication.init();
}

// SpringBoot 2.x 自定义端口方式
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(8080);
}

// 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例
private static ApplicationContext APPLICATION_CONTEXT;
Expand Down
Expand Up @@ -101,16 +101,31 @@
@Service
@RestController
@RequestMapping("")
public class DemoController extends APIJSONController {
public class DemoController extends APIJSONController<Long> {
private static final String TAG = "DemoController";

// 可以更方便地通过日志排查错误
@Override
public String getRequestURL() {
return httpServletRequest.getRequestURL().toString();
}


//通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// 通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

/**增删改查统一入口,这个一个方法可替代以下 7 个方法,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param tag
* @param params
* @param request
* @param session
* @return
*/
@PostMapping(value = "crud/{method}") // 直接 {method} 或 apijson/{method} 会和内置网页的路由有冲突
@Override
public String crud(@PathVariable String method, @RequestBody String request, HttpSession session) {
return super.crud(method, request, session);
}

/**获取
* @param request 只用String,避免encode后未decode
Expand Down Expand Up @@ -196,8 +211,23 @@ public String delete(@RequestBody String request, HttpSession session) {
return super.delete(request, session);
}

// 通用接口,非事务型操作 和 简单事务型操作 都可通过这些接口自动化实现 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//以上接口对应的简版接口,格式为 {method}/{tag}?format=true&@explain=true.. <<<<<<<<<<<<<<<<<<<<<<<<<
// 以上接口对应的简版接口,格式为 {method}/{tag}?format=true&@explain=true.. <<<<<<<<<<<<<<<<<<<<<<<<<

/**增删改查统一接口,这个一个接口可替代 7 个万能通用接口,牺牲一些路由解析性能来提升一点开发效率
* @param method
* @param tag
* @param params
* @param request
* @param session
* @return
*/
@PostMapping("crud/{method}/{tag}") // 直接 {method}/{tag} 或 apijson/{method}/{tag} 会和内置网页的路由有冲突
@Override
public String crudByTag(@PathVariable String method, @PathVariable String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
return super.crudByTag(method, tag, params, request, session);
}

/**获取
* @param request 只用String,避免encode后未decode
Expand Down Expand Up @@ -283,7 +313,7 @@ public String deleteByTag(@PathVariable String tag, @RequestParam Map<String, St
return super.deleteByTag(tag, params, request, session);
}

//以上接口对应的简版接口,格式为 {method}/{tag}?format=true&@explain=true.. >>>>>>>>>>>>>>>>>>>>>>>>>
// 以上接口对应的简版接口,格式为 {method}/{tag}?format=true&@explain=true.. >>>>>>>>>>>>>>>>>>>>>>>>>



Expand Down Expand Up @@ -1231,6 +1261,7 @@ else if (APIJSON_DELEGATE_ID.toLowerCase().equals(name.toLowerCase())) {
map.remove("$_type");
map.remove("$_except_headers");
map.remove("$_delegate_url");
map.remove("$_delegate_id");

Set<Entry<String, String[]>> set = map == null ? null : map.entrySet();

Expand Down
Expand Up @@ -57,7 +57,9 @@ public Long getCurrentUserId(@NotNull JSONObject current) {
}

public List<Long> getCurrentUserIdAsList(@NotNull JSONObject current) {
return Arrays.asList(DemoVerifier.getVisitorId(getSession()));
List<Long> list = new ArrayList<>(1);
list.add(DemoVerifier.getVisitorId(getSession()));
return list;
}

public List<Long> getCurrentContactIdList(@NotNull JSONObject current) {
Expand Down Expand Up @@ -235,17 +237,14 @@ public JSONArray getIdList(@NotNull JSONObject current) {
* @return
* @throws Exception
*/
@Override
public Object verifyAccess(@NotNull JSONObject current) throws Exception {
long userId = current.getLongValue(JSONRequest.KEY_USER_ID);
String role = current.getString(JSONRequest.KEY_ROLE);
if (AbstractVerifier.OWNER.equals(role) && userId != DemoVerifier.getVisitorId(getSession())) {
if (AbstractVerifier.OWNER.equals(role) && userId != (Long) DemoVerifier.getVisitorId(getSession())) {
throw new IllegalAccessException("登录用户与角色OWNER不匹配!");
}
return null;
}




}
Expand Up @@ -26,7 +26,7 @@
* 具体见 https://github.com/Tencent/APIJSON/issues/38
* @author Lemon
*/
public class DemoParser extends APIJSONParser {
public class DemoParser extends APIJSONParser<Long> {

public DemoParser() {
super();
Expand Down
Expand Up @@ -65,10 +65,10 @@ public DemoSQLConfig(RequestMethod method, String table) {
// TABLE_KEY_MAP.put(Privacy.class.getSimpleName(), "apijson_privacy");

//主键名映射
SIMPLE_CALLBACK = new SimpleCallback() {
SIMPLE_CALLBACK = new SimpleCallback<Long>() {

@Override
public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table) {
public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table) {
return new DemoSQLConfig(method, table);
}

Expand All @@ -81,13 +81,13 @@ public AbstractSQLConfig getSQLConfig(RequestMethod method, String database, Str
// }

@Override
public String getUserIdKey(String database, String schema,String datasource, String table) {
public String getUserIdKey(String database, String schema, String datasource, String table) {
return USER_.equals(table) || PRIVACY_.equals(table) ? ID : USER_ID; // id / userId
}

//取消注释来实现数据库自增 id
// @Override
// public Object newId(RequestMethod method, String database, String schema, String datasource, String table) {
// public Long newId(RequestMethod method, String database, String schema, String datasource, String table) {
// return null; // return null 则不生成 id,一般用于数据库自增 id
// }

Expand Down
Expand Up @@ -21,7 +21,7 @@
* 具体见 https://github.com/Tencent/APIJSON/issues/12
* @author Lemon
*/
public class DemoVerifier extends APIJSONVerifier {
public class DemoVerifier extends APIJSONVerifier<Long> {
public static final String TAG = "DemoVerifier";

// 重写方法来自定义字段名等
Expand Down
17 changes: 11 additions & 6 deletions APIJSON-Java-Server/APIJSONDemo-Druid/pom.xml
Expand Up @@ -5,7 +5,7 @@

<groupId>apijson.demo</groupId>
<artifactId>apijson-demo-druid</artifactId>
<version>5.0.0</version>
<version>5.1.0</version>
<packaging>jar</packaging>

<name>APIJSONDemo-Druid</name>
Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>com.github.APIJSON</groupId>
<artifactId>apijson-framework</artifactId>
<version>LATEST</version>
<version>5.1.0</version>
</dependency>

<!-- 需要用的数据库 JDBC 驱动 -->
Expand Down Expand Up @@ -83,6 +83,15 @@
</build>

<repositories>
<!-- APIJSON 必须用到的托管平台 -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>

<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
Expand All @@ -94,10 +103,6 @@
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

</project>
Expand Up @@ -32,7 +32,6 @@
import apijson.framework.APIJSONApplication;
import apijson.framework.APIJSONCreator;
import apijson.orm.SQLConfig;
import apijson.orm.SQLExecutor;


/**Demo SpringBoot Application 主应用程序启动类
Expand Down Expand Up @@ -63,15 +62,11 @@ public void customize(ConfigurableServletWebServerFactory server) {

static {
// 使用本项目的自定义处理类
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator() {
APIJSONApplication.DEFAULT_APIJSON_CREATOR = new APIJSONCreator<Long>() {
@Override
public SQLConfig createSQLConfig() {
return new DemoSQLConfig();
}
@Override
public SQLExecutor createSQLExecutor() {
return new DemoSQLExecutor();
}
};

// 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
Expand Down

0 comments on commit ee020d3

Please sign in to comment.