您好,登錄后才能下訂單哦!
這篇文章主要講解了“springcloud feign服務(wù)間調(diào)用date類型轉(zhuǎn)換錯(cuò)誤怎么解決”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“springcloud feign服務(wù)間調(diào)用date類型轉(zhuǎn)換錯(cuò)誤怎么解決”吧!
最近嘗試換springcloud開發(fā),原先是springboot,每次的返回值的Date類型都通過@ControllerAdvice格式化yyyy-MM-dd HH:mm:ss然后返回的。這次用feign之后,2個(gè)服務(wù)之間調(diào)用,一直報(bào)錯(cuò)查了好久百度都搞不定,后面靈光一閃。。。不多說,上方案
標(biāo)識(shí)只有feign請(qǐng)求會(huì)多一個(gè)flag頭
@Configuration public class FeignHeaderConfig implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String token = request.getHeader(C.API_HEADER_PARAM_TOKEN); String session = request.getHeader(C.API_HEADER_PARAM_SESSION); String timestamp = request.getHeader(C.API_HEADER_PARAM_TIMESTAMP); requestTemplate.header(C.API_HEADER_PARAM_TOKEN, token); requestTemplate.header(C.API_HEADER_PARAM_SESSION,session); requestTemplate.header(C.API_HEADER_PARAM_TIMESTAMP,timestamp); requestTemplate.header("flag","feign");//標(biāo)識(shí)feign請(qǐng)求 } }
再?zèng)Q定是否需要格式化時(shí)間
@ControllerAdvice @Slf4j public class RespondeBodyConfig implements ResponseBodyAdvice { @Override public Object beforeBodyWrite(Object resBody, MethodParameter arg1, MediaType arg2, Class arg3, ServerHttpRequest req, ServerHttpResponse res) { //解決feign之間調(diào)用格式化,導(dǎo)致date類型轉(zhuǎn)換錯(cuò)誤 List<String> flag = req.getHeaders().get("flag"); if(flag != null && flag.size() > 0 && flag.get(0).equals("feign")){ return resBody; } //resBody就是controller方法中返回的值,對(duì)其進(jìn)行修改后再return就可以了 ObjectMapper mapper = new ObjectMapper(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat(format); try { if(resBody instanceof TResult) { String json = mapper.writeValueAsString(resBody); return JSONObject.parseObject(json, TResult.class); } } catch (Exception e) { log.error("【全局返回值處理失敗】",e); } return resBody; } @Override public boolean supports(MethodParameter arg0, Class arg1) { //這里直接返回true,表示對(duì)任何handler的responsebody都調(diào)用beforeBodyWrite方法 return true; } }
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2021-07-27 10:35:16": not a valid representation (error: Failed to parse Date value '2021-07-27 10:35:16': Cannot parse date "2021-07-27 10:35:16": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
使用OpenFeign進(jìn)行服務(wù)間調(diào)用時(shí)返回的對(duì)象中時(shí)間類型為字符串型,接收的對(duì)象對(duì)應(yīng)的類型為Date類型,openFeign轉(zhuǎn)換日期格式時(shí)異常
1.配置文件中加
spring.jackson.timeZone=GMT+08:00 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.serialization.write_dates_as_timestamps=false spring: jackson: timeZone: GMT+08:00 date-format: yyyy-MM-dd HH:mm:ss serialization.write_dates_as_timestamps: false
2、字段上加一下注解
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime;
感謝各位的閱讀,以上就是“springcloud feign服務(wù)間調(diào)用date類型轉(zhuǎn)換錯(cuò)誤怎么解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)springcloud feign服務(wù)間調(diào)用date類型轉(zhuǎn)換錯(cuò)誤怎么解決這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。