Convert RestTemplate json to entity class
Sometimes we need to use RestTemplate to access other url resources in java server, but because it is a class in two servers (JVMs), how to transfer entity classes?
- Contract entity class
This example returns the entity class with AgreementApproveForOA as the result - Code to accept request
@ApiOperation(value = "Get framework agreement") @PostMapping("/getAgreementApprove") public ResponseEntity<AgreementApproveDTO> getAgreementApprove(@RequestParam Integer fcompanyId, @RequestParam Integer fcompanyType, @RequestHeader("fuid") Integer faid) { return xxx; }
- Transformation instance
public static AgreementApproveForOA getData() throws IOException { HttpHeaders headers = new HttpHeaders(); headers.add("fuid", "2");//Set header String url = "http://localhost:8481/admin/admin/agreementApprove/getAgreementApprove?fcompanyId=174&fcompanyType=1"; ResponseEntity<String> data = restTemplate.postForEntity(url, new HttpEntity<String>(headers), String.class);//Get json string AgreementResponseEntity agreementResponseEntity; //readValue(json string, which corresponds to the assembled entity class) agreementResponseEntity = mapper.readValue(data.getBody(), AgreementResponseEntity.class); return agreementResponseEntity.getRetEntity(); }
- Attach dependency pom.xml
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.1.10.RELEASE</version> </dependency>
- Trampled pit
- The jackson package does not support the format of 2019-01-05 13:25:10. The following errors will occur
Cannot deserialize value of type `java.util.Date` from String "2019-10-03 00:00:00": not a valid representation (error: Failed to parse Date value '2019-10-03 00:00:00': Cannot parse date "2019-10-03 00:00:00": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))
- Solution: custom date resolver
The solution is quoted from: link
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder(); mapper = jackson2ObjectMapperBuilder.build(); DateFormat dateFormat = mapper.getDateFormat(); mapper.setDateFormat(new MyDateFormat(dateFormat));
MyDateFormat.java
public class MyDateFormat extends DateFormat { private DateFormat dateFormat; private SimpleDateFormat format1 = new SimpleDateFormat("yyy-MM-dd HH:mm:ss"); public MyDateFormat(DateFormat dateFormat) { this.dateFormat = dateFormat; } @Override public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) { return dateFormat.format(date, toAppendTo, fieldPosition); } @Override public Date parse(String source, ParsePosition pos) { Date date = null; try { date = format1.parse(source, pos); } catch (Exception e) { date = dateFormat.parse(source, pos); } return date; } // Mainly decoration @Override public Date parse(String source) throws ParseException { Date date = null; try { // Follow my rules first date = format1.parse(source); } catch (Exception e) { // No, then follow the original rules date = dateFormat.parse(source); } return date; } // The reason for decorating the clone method here is that the clone method is also useful in jackson. @Override public Object clone() { Object format = dateFormat.clone(); return new MyDateFormat((DateFormat) format); } }
In this way, you can turn json into the corresponding entity class.