调用其他服务API,得到的响应包比如:
expirse_in,
errcode,
userid
不满足java驼峰命名。
要怎么转换:
响应体responseDto:
@Data
public class PermanentCodeBean implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty(value = "errcode")
private String errcode;
@JsonProperty(value = "errmsg")
private String errmsg;
@JsonProperty(value = "access_token")
private String accessToken;
@JsonProperty(value = "expires_in")
private String expiresIn;
@JsonProperty(value = "permanent_code")
private String permanentCode;
@JsonProperty(value = "dealer_corp_info")
private DealerCorpInfoBean dealerCorpInfoBean;
@JsonProperty(value = "auth_corp_info")
private AuthCorpInfoBean authCorpInfo;
@JsonProperty(value = "auth_info")
private AuthInfoBean authInfo;
@JsonProperty(value = "auth_user_info")
private AuthUserInfoBean authUserInfo;
@JsonProperty(value = "register_code_info")
private RegisterCodeInfoBean registerCodeInfo;
private String state;
}
把驼峰命名风格的实体类,转换为mysql中下划线的字段。怎么转换。
entity 实体类,默认驼峰命名在转成mysql 的值时, 就会换成下划线。
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("auth_user_t")
public class AuthUserEntity extends BaseEntity{
private static final long serialVersionUID = 1L;
/**
* 用户所属企业的corpid
*/
private String corpId;
/**
* 第三方应用id
*/
private String suiteId;
/**
* 用户在企业内的UserID
*/
private String userId;
/**
* open_userid 全局唯一。对于同一个服务商,不同应用获取到企业内同一个成员的open_userid是相同的,最多64个字节。仅第三方应用可获取
*/
private String openUserId;
/**
* 成员票据 scope为snsapi_userinfo或snsapi_privateinfo,且用户在应用可见范围之内时返回此参数。 后续利用该参数可以获取用户信息或敏感信息,参见"第三方使用user_ticket获取成员详情"。
*/
private String userTicket;
/**
* user_ticket的有效时间(秒) 随user_ticket一起返回
*/
private int expiresIn;
/**
* 成员票据更新时间
*/
private LocalDateTime updateTicketTime;
}
mapstruct 怎么用?
1: 引入
<!--对象转换工具-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</dependency>
2:定义一个接口
@Mapper(componentModel = "spring")
public interface JsapiTicketConvertor {
@Mapping(target = "enterpriseTicket", source = "ticketVo.ticket")
@Mapping(target = "corpId", source = "ticketVo.corpId")
@Mapping(target = "agentId", source = "ticketVo.agentId")
@Mapping(target = "suiteId", source = "appId")
@Mapping(target = "updateEnterpriseTicketTime", expression = "java(java.time.LocalDateTime.now(java.time.ZoneId.of(\"+8\")))")
@Mapping(target = "enterpriseExpiresIn", source = "ticketVo.expires_in")
JsapiTicketEntity enterpriseVoToEntity(JsapiTicketVo ticketVo, String appId);
}
类上的Mapper注解,如果不加componentModel, 就没法把转换器注入到spring容器里。
Description:
Field ticketConvertor in com.shanhaiping.medicalmaterial.web.ability.JsapiTicketAbility required a bean of type 'com.shanhaiping.medicalmaterial.mapstruct.JsapiTicketConvertor' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.shanhaiping.medicalmaterial.mapstruct.JsapiTicketConvertor' in your configuration.
因为JsapiTicketConvertor是在运行时框架帮助实现的,如果不注入交给spring容器,就要自己实现转换器。然后引用时, 创建一个new 转换器。
4:自定义:
@Mapping(target = "updateEnterpriseTicketTime", expression = "java(java.time.LocalDateTime.now(java.time.ZoneId.of(\"+8\")))")
express 里面的引用类型,要写全类名。