响应类型包装

This commit is contained in:
shikong 2023-08-18 17:15:31 +08:00
parent 8a1e514aa0
commit b083222690
5 changed files with 40 additions and 6 deletions

View File

@ -64,6 +64,17 @@
<artifactId>springdoc-openapi-starter-common</artifactId>
</dependency>
<!--MapStruct-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -14,12 +14,12 @@ public class ZlmResponse<T> {
@Schema(title = "状态码")
private ResponseStatus code;
@Schema(title = "响应消息")
private String msg;
@Schema(title = "响应数据")
private T data;
@Schema(title = "响应消息")
private String msg;
@Override
public String toString(){
return JsonUtils.toJson(this);

View File

@ -0,0 +1,18 @@
package cn.skcks.docking.gb28181.media.dto.response;
import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.media.dto.status.ResponseStatus;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface ZlmResponseConvertor {
ZlmResponseConvertor INSTANCE = Mappers.getMapper(ZlmResponseConvertor.class);
default <T> JsonResponse<T> toJsonResponse(ZlmResponse<T> response){
return JsonResponse.build(response.getData(), response.getCode().getCode(), response.getMsg());
}
default <T> ZlmResponse<T> toZlmResponse(JsonResponse<T> response){
return new ZlmResponse<>(ResponseStatus.fromCode(response.getCode()), response.getData(), response.getMsg());
}
}

View File

@ -21,7 +21,7 @@ public enum ResponseStatus {
private final String msg;
@JsonCreator
public ResponseStatus fromCode(int code){
public static ResponseStatus fromCode(int code){
for (ResponseStatus status : values()) {
if (status.getCode() == code) {
return status;

View File

@ -7,6 +7,7 @@ import cn.skcks.docking.gb28181.common.json.JsonResponse;
import cn.skcks.docking.gb28181.media.config.ZlmMediaConfig;
import cn.skcks.docking.gb28181.media.dto.config.ServerConfig;
import cn.skcks.docking.gb28181.media.dto.response.ZlmResponse;
import cn.skcks.docking.gb28181.media.dto.response.ZlmResponseConvertor;
import cn.skcks.docking.gb28181.media.proxy.ZlmMediaHttpService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@ -42,8 +43,12 @@ public class MediaServiceTest {
log.info("{}", entity.getBody());
ZlmResponse<List<ServerConfig>> test = zlMediaHttpService.getServerConfig(secret);
log.info("{}", test);
log.info("{}",test.getCode().getMsg());
JsonResponse<List<ServerConfig>> jsonResponse = ZlmResponseConvertor.INSTANCE.toJsonResponse(test);
log.info("{}", jsonResponse);
ZlmResponse<List<ServerConfig>> zlmResponse = ZlmResponseConvertor.INSTANCE.toZlmResponse(jsonResponse);
log.info("{}", zlmResponse);
log.info("{}", zlmResponse.getCode().getMsg());
}
@Test