fix:利盟服务返回值中的数据增加使用泛型进行接收

This commit is contained in:
石崇礼 2025-02-08 17:20:50 +08:00
parent fc1e02264d
commit ed61a25ac4
3 changed files with 51 additions and 17 deletions

View File

@ -1,5 +1,6 @@
package com.dpkj.common.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -18,7 +19,7 @@ import java.io.Serializable;
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class LexMarkResultDTO implements Serializable {
public class LexMarkResultDTO <T> implements Serializable {
/**
@ -46,21 +47,27 @@ public class LexMarkResultDTO implements Serializable {
*/
private String cmdName;
/**
* 返回参数
*/
private Param param;
/**
* 详情描述
*/
private String desc;
/**
* 利盟服务返回的参数
*/
@JsonIgnore
private String param;
/**
* 实际返回的接受的参数对象通过param转换过后的
*/
private T data;
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Param { // 不同的actionName导致返回值不同需要的参数可以加上
public static class Param {
/**
* 请求ID
@ -102,5 +109,7 @@ public class LexMarkResultDTO implements Serializable {
*/
private String desc;
}
}

View File

@ -1,13 +1,14 @@
package com.dpkj.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dpkj.common.dto.LexMarkDTO;
import com.dpkj.common.dto.LexMarkResultDTO;
import com.dpkj.common.exception.RRException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
@ -35,10 +36,11 @@ public class ThirdService {
/**
* 利盟台式机-立体机 接口请求
*
* @param lexMarkDTO 请求DTO
* @return result
*/
public LexMarkResultDTO callDevice(LexMarkDTO lexMarkDTO) {
public <T> LexMarkResultDTO<T> callDevice(LexMarkDTO lexMarkDTO, Class<T> clazz) {
try {
URL url = new URL(lexMarkServiceIp + ":" + lexMarkServicePort + "/CallDevice");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
@ -55,7 +57,7 @@ public class ThirdService {
}
int responseCode = connection.getResponseCode();
if (responseCode!= HttpURLConnection.HTTP_OK) {
if (responseCode != HttpURLConnection.HTTP_OK) {
log.error("利盟服务请求失败,响应码:{},请求参数:{}", responseCode, lexMarkDTO);
throw new RRException("利盟服务请求失败,响应码:" + responseCode);
}
@ -63,27 +65,50 @@ public class ThirdService {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine())!= null) {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 将响应JSON字符串转换为LexMarkResultDTO对象
LexMarkResultDTO lexMarkResultDTO = JSON.parseObject(response.toString(), LexMarkResultDTO.class);
if( lexMarkResultDTO.getResult() != 0){
LexMarkResultDTO<T> lexMarkResultDTO = JSON.parseObject(response.toString(), LexMarkResultDTO.class);
if (lexMarkResultDTO.getResult() != 0) {
log.error("利盟服务请求出错:{}", lexMarkResultDTO);
throw new RRException(lexMarkResultDTO.toString());
}
// 实例化param位data
String param = lexMarkResultDTO.getParam();
if (!StringUtils.isEmpty(param)) {
try {
T t = JSON.parseObject(param, clazz);
lexMarkResultDTO.setData(t);
}catch (Exception e){
log.error("类型转换失败");
throw new RRException("转换param位data时类型与实际类型不匹配");
}
}
return lexMarkResultDTO;
}catch (Exception e){
} catch (Exception e) {
log.error("利盟服务请求失败:{}", lexMarkDTO, e);
if ( e instanceof RRException ){
if (e instanceof RRException) {
throw new RRException(((RRException) e).getCode(), e.getMessage());
}
throw new RRException("利盟服务请求失败");
}
}
/**
* 数据对象的data采用JSONObject进行接收
*
* @param lexMarkDTO 请求参数
* @return result
*/
public LexMarkResultDTO<JSONObject> callDevice(LexMarkDTO lexMarkDTO) {
return callDevice(lexMarkDTO, JSONObject.class);
}
}

View File

@ -26,7 +26,7 @@ public class RegisterServiceImpl implements PrintService {
private ThirdService thirdService;
@Override
public LexMarkResultDTO printImage(JSONObject data, String template, int width, int height, String saveDir) {
public LexMarkResultDTO<LexMarkResultDTO.Param> printImage(JSONObject data, String template, int width, int height, String saveDir) {
StringBuilder filePath = new StringBuilder(saveDir);
// 校验是否选中了模板,如果没选中模板的话则不需要另外生成了
if ( !StringUtils.isEmpty(template) && !StringUtils.isEmpty(saveDir)){
@ -48,7 +48,7 @@ public class RegisterServiceImpl implements PrintService {
param.put("mediaCtrl", 1);
param.put("fields", "LOGO=" + filePath);
lexMarkDTO.setParam(param.toJSONString());
return thirdService.callDevice(lexMarkDTO);
return thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class);
}
}