Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.7.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.github/workflows/publish-snapshot.yml
#	CHANGELOG.md
#	README.md
#	README_EN.md
#	build.gradle
#	deploy.sh
#	gradle.properties
#	mica-activerecord/build.gradle
#	mica-bom/build.gradle
#	mica-captcha/build.gradle
#	mica-captcha/src/main/java/net/dreamlu/mica/captcha/config/MicaCaptchaAutoConfiguration.java
#	mica-ip2region/build.gradle
#	mica-ip2region/src/main/java/net/dreamlu/mica/ip2region/core/IpAddress.java
#	mica-redis/src/main/java/net/dreamlu/mica/redis/cache/MicaRedisCache.java
#	mica-xss/src/main/java/net/dreamlu/mica/xss/core/DefaultXssCleaner.java
  • Loading branch information
li-xunhuan committed Aug 21, 2023
2 parents 37a5ec8 + 3b02dac commit 00901bf
Show file tree
Hide file tree
Showing 22 changed files with 1,705 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ public static String toJson(@Nullable Object object) {
}
}

/**
* 将对象序列化成json字符串
*
* @param object javaBean
* @param serializationView serializationView
* @return jsonString json字符串
*/
@Nullable
public static String toJsonWithView(@Nullable Object object, Class<?> serializationView) {
if (object == null) {
return null;
}
try {
return getInstance()
.writerWithView(serializationView)
.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw Exceptions.unchecked(e);
}
}

/**
* 将对象序列化成 json 字符串,格式美化
*
Expand All @@ -75,7 +96,9 @@ public static String toPrettyJson(@Nullable Object object) {
return null;
}
try {
return getInstance().writerWithDefaultPrettyPrinter().writeValueAsString(object);
return getInstance()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw Exceptions.unchecked(e);
}
Expand All @@ -98,6 +121,25 @@ public static byte[] toJsonAsBytes(@Nullable Object object) {
}
}

/**
* 将对象序列化成 json byte 数组
*
* @param object javaBean
* @return jsonString json字符串
*/
public static byte[] toJsonAsBytesWithView(@Nullable Object object, Class<?> serializationView) {
if (object == null) {
return new byte[0];
}
try {
return getInstance()
.writerWithView(serializationView)
.writeValueAsBytes(object);
} catch (JsonProcessingException e) {
throw Exceptions.unchecked(e);
}
}

/**
* 将json字符串转成 JsonNode
*
Expand Down
73 changes: 73 additions & 0 deletions mica-core/src/test/java/net/dreamlu/mica/test/utils/JsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package net.dreamlu.mica.test.utils;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.mica.core.utils.JsonUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* json 测试
*
* @author L.cm
*/
class JsonTest {
private static final ObjectMapper objectMapper = new ObjectMapper();

public static class Views {
public static class Public { }
public static class Internal extends Public { }
}

@Data
public static class User {
@JsonView(Views.Public.class)
private Long id;

@JsonView(Views.Public.class)
private String name;

@JsonView(Views.Internal.class)
private String email;
}

@Test
void test1() throws JsonProcessingException {
User user = new User();
user.setId(System.currentTimeMillis());
user.setName("张三");
user.setEmail("[email protected]");
String json1 = objectMapper.writeValueAsString(user);
Assertions.assertTrue(json1.contains("zhangsan"));
String json2 = objectMapper.writerWithView(Views.Public.class).writeValueAsString(user);
Assertions.assertFalse(json2.contains("zhangsan"));
}

@Test
void test2() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setName("张三");
user.setEmail("[email protected]");
String json1 = JsonUtil.toJson(user);
Assertions.assertTrue(json1.contains("zhangsan"));
String json2 = JsonUtil.toJsonWithView(user, Views.Public.class);
Assertions.assertFalse(json2.contains("zhangsan"));
}

@Test
void testR() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setName("张三");
user.setEmail("[email protected]");
String json1 = JsonUtil.toJson(R.success(user));
Assertions.assertTrue(json1.contains("zhangsan"));
String json2 = JsonUtil.toJsonWithView(R.success(user), Views.Public.class);
Assertions.assertFalse(json2.contains("zhangsan"));
}

}
4 changes: 4 additions & 0 deletions mica-nats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## mica-nats(高性能、轻量级消息队列)

nats 封装,更改方便 Spring boot 下使用。

9 changes: 9 additions & 0 deletions mica-nats/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {
api project(":mica-core")
api "io.nats:jnats:2.16.13"
implementation "jakarta.validation:jakarta.validation-api"
implementation "org.springframework.boot:spring-boot-starter"
compileOnly "org.springframework.cloud:spring-cloud-context"
compileOnly "net.dreamlu:mica-auto:${micaAutoVersion}"
annotationProcessor "net.dreamlu:mica-auto:${micaAutoVersion}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dreamlu.mica.nats.annotation;

import java.lang.annotation.*;

/**
* nats 监听器
*
* @author L.cm
*/
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NatsListener {

/**
* 主题 subject
*
* @return subject
*/
String value();

/**
* 队列
* @return 队列名称
*/
String queue() default "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 ([email protected] & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.dreamlu.mica.nats.annotation;

import java.lang.annotation.*;

/**
* nats stream 监听器注解
*
* @author L.cm
*/
@Documented
@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NatsStreamListener {

/**
* 主题 subject
*
* @return subject
*/
String value();

/**
* 队列
* @return 队列名称
*/
String queue() default "";

/**
* 交付主题
*/
String deliverSubject() default "";

/**
* 交付组
*/
String deliverGroup() default "";

}
Loading

0 comments on commit 00901bf

Please sign in to comment.