Skip to content

Commit

Permalink
Add module extension-solon & example-solon-test (#2996)
Browse files Browse the repository at this point in the history
* add new method JSONFactory.getDefaultReaderFeatures(),JSONFactory.getDefaultWriterFeatures(),

* add new method JSONFactory.getDefaultWriterZoneId(),JSONFactory.getDefaultReaderZoneId(),

* add new method JSONFactory.getDefaultReaderFormat(),

* remove a blank line

* Add module extension-solon & example-solon-test

* Beautify module extension-solon comments

* Beautify module example-solon-test readme

* Beautify module example-solon-test pom.xml

* Adjusting module ordering

* fix fastjson2-extension-solon checkstyle list

* fix example-solon-test checkstyle list

* fix fastjson2-extension-solon checkstyle list

* fix fastjson2-extension-solon remove the requirement case2 test

* Optimize fastjson2-extension-solon code and comments
  • Loading branch information
noear authored Oct 8, 2024
1 parent 4e2dbd8 commit 3de60a9
Show file tree
Hide file tree
Showing 40 changed files with 1,649 additions and 0 deletions.
20 changes: 20 additions & 0 deletions example-solon-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### 启动方式

运行 SolonTestApp.main 方法(或者直接运行单测 DemoTest )

### 测试

测试 Render 是否正常工作

* 访问:[http://localhost:8080/demo?username=world&password=1234](http://localhost:8080/demo?username=world&password=1234)

测试 找不到的地址异常(技术上和上面一样,Solon 所有的 Web 输出都会走:Render 接口)

* 访问:[http://localhost:8080/error](http://localhost:8080/error)

### 说明

此演示,会自动触发 fastjson2-extension-solon 的:

* Fastjson2RenderFactory
* Fastjson2ActionExecutor
104 changes: 104 additions & 0 deletions example-solon-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-parent</artifactId>
<version>2.0.54-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>com.com.alibaba.fastjson2</groupId>
<artifactId>example-solon-test</artifactId>
<name>example-solon-test</name>
<description>example-solon-test</description>

<properties>
<java.version>8</java.version>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.test.skip>true</maven.test.skip>
</properties>

<!-- 使用 aliyun 的 Maven 源,提升下载速度 -->
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>

<repository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>aliyunmaven</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</pluginRepository>
</pluginRepositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-parent</artifactId>
<type>pom</type>
<version>${solon.version}</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-solon</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-boot-jdkhttp</artifactId>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-logging-simple</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.noear</groupId>
<artifactId>solon-maven-plugin</artifactId>
<version>${solon.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.alibaba.fastjson2.example.solontest;

import org.noear.solon.Solon;
import org.noear.solon.annotation.SolonMain;

@SolonMain
public class SolonTestApp {
public static void main(String[] args) {
Solon.start(SolonTestApp.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.fastjson2.example.solontest.config;

import com.alibaba.fastjson2.support.solon.Fastjson2ActionExecutor;
import com.alibaba.fastjson2.support.solon.Fastjson2RenderFactory;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;

/**
* @author noear
* @since 2024-10-01
*/
@Configuration
public class JsonConfigurer {
@Bean
public void fastjson2(Fastjson2ActionExecutor executor, Fastjson2RenderFactory render) {
// executor.config().config(
// JSONReader.Feature.FieldBased,
// JSONReader.Feature.SupportArrayToBean);
//
// render.addFeatures(
// JSONWriter.Feature.WriteMapNullValue,
// JSONWriter.Feature.PrettyFormat);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.fastjson2.example.solontest.controller;

import com.alibaba.fastjson2.example.solontest.entity.User;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;

/**
* @author noear
* @since 2024-10-01
*/
@Controller
public class DemoController {
@Mapping("/demo")
public User demo(User user) {
return user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.fastjson2.example.solontest.controller;

import org.noear.solon.annotation.Component;
import org.noear.solon.core.exception.StatusException;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
import org.noear.solon.core.handle.Result;

/**
* @author noear
* @since 2024-10-01
*/
@Component
public class DemoErrorFilter
implements Filter {
@Override
public void doFilter(Context ctx, FilterChain chain) throws Throwable {
try {
chain.doFilter(ctx);
} catch (StatusException e) {
ctx.render(Result.failure(e.getCode(), e.getMessage()));
} catch (Throwable e) {
ctx.render(Result.failure(500));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.fastjson2.example.solontest.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author noear
* @since 2024-10-01
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String username;
private String password;
}
3 changes: 3 additions & 0 deletions example-solon-test/src/main/resources/app.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server.port=8080

solon.logging.logger.root.level=INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.alibaba.fastjson2.example.solontest;

import org.junit.jupiter.api.Test;
import org.noear.solon.test.HttpTester;
import org.noear.solon.test.SolonTest;

/**
* @author noear 2024/10/2 created
*/
@SolonTest(SolonTestApp.class)
public class DemoTest
extends HttpTester {
@Test
public void ok_post_json() throws Exception {
String json = "{\"password\":\"1234\",\"username\":\"world\"}";

String json2 = path("/demo").bodyJson(json).post();

assert json.equals(json2);
}

@Test
public void ok_get() throws Exception {
String json = "{\"password\":\"1234\",\"username\":\"world\"}";

String json2 = path("/demo?username=world&password=1234").get();

assert json.equals(json2);
}

@Test
public void error() throws Exception {
String json = "{\"code\":404,\"description\":\"Not Found: GET /error\"}";

String json2 = path("/error").get();

assert json.equals(json2);
}
}
100 changes: 100 additions & 0 deletions extension-solon/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-parent</artifactId>
<version>2.0.54-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>fastjson2-extension-solon</artifactId>
<name>fastjson2-extension-solon</name>
<description>Fastjson is a JSON processor (JSON parser + JSON generator) written in Java</description>
<packaging>jar</packaging>
<url>https://github.com/alibaba/fastjson2</url>
<inceptionYear>2024</inceptionYear>

<licenses>
<license>
<name>Apache 2</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<scm>
<url>https://github.com/alibaba/fastjson2</url>
<connection>scm:git:https://[email protected]/alibaba/fastjson2.git</connection>
</scm>
<organization>
<name>Alibaba Group</name>
<url>https://github.com/alibaba</url>
</organization>
<developers>
<developer>
<id>wenshao</id>
<name>wenshao</name>
<email>shaojin.wensj(at)alibaba-inc.com</email>
<roles>
<role>Developer</role>
<role>Tech Leader</role>
</roles>
<timezone>+8</timezone>
<url>https://github.com/wenshao</url>
</developer>
<developer>
<id>noear</id>
<name>noear</name>
<email>[email protected]</email>
<timezone>+8</timezone>
<url>https://github.com/noear</url>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-serialization</artifactId>
<version>${solon.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-logging-simple</artifactId>
<version>${solon.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-test</artifactId>
<version>${solon.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>com/alibaba/fastjson2/**/*.java</include>
</includes>
<systemPropertyVariables>
<user.timezone>Asia/Shanghai</user.timezone>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 3de60a9

Please sign in to comment.