In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to write logic with Java8". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Business background
First of all, the business requirements are like this: to pull all orders from a third-party e-commerce platform and save them to the company's own database, we need to judge whether there is logistics information, and if there is logistics information, we need to upload it again.
The data returned by the third-party interface is in JSON format, but the logistics information is hidden very deeply. As shown below, the JSON node is like this:
XxxOrder > xxxShippingInfo > xxxShipmentDetails > xxxTrackingInfo > trackingNumber, trackingLink
Basic realization
Because the data returned by the third-party interface is in JSON format, the JSON string needs to be converted into a Java object for processing.
@ JsonIgnoreProperties (ignoreUnknown = true) public class XxxOrder {/ * Logistics Information * / @ JsonProperty ("shippingInfo") private XxxShippingInfo xxxShippingInfo;}
The above is only an example of the first layer. To get logistics information, you need to encapsulate four layers of objects in turn, and to avoid null pointers when you really obtain logistics information, you need to judge four layers to get it, as shown in the example:
If (xxxOrder! = null) {if (xxxOrder.getXxxShippingInfo ()! = null) {if (xxxOrder.getXxxShippingInfo (). GetXxxShipmentDetails ()! = null) {if (xxxOrder.getXxxShippingInfo (). GetXxxShipmentDetails (). GetXxxTrackingInfo ()! = null) {.}}
It's so troublesome to get a logistics information, I'm also drunk, and it's too inelegant to write like this.
Java 8 implementation
Because I know that Java 8 can handle this kind of requirements, I never wanted to implement it in the most primitive way. I just implemented it in Java 8:
Official account: Java technology stack / private String [] getFulfillments (XxxOrder xxxOrder) {return Optional.ofNullable (xxxOrder) .map ((o)-> o.getXxxShippingInfo ()) .map ((si)-> si.getXxxShipmentDetails ()) .map ((sd)-> sd.getXxxTrackingInfo ()) .map ((t)-> new String [] {t.getTrackingNumber ()) T.getTrackingLink ()}) .orElse (null) }
After writing, my colleagues even said that they could not understand it, and they came to ask me specially.
Realization principle
In fact, this does not use any superb technology, that is, the use of Java 8 Optional to achieve, the details will not be introduced, mainly to avoid null pointers, do not understand can click here to view this article.
Today, let's introduce the principle of Optional#map method to implement this logic, and take a look at the implementation source code of map:
Public Optional map (Function
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.