RestTemplate json into solids based

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/klli15/article/details/102753982

RestTemplate json into solids based

Sometimes we need to use RestTemplate url to access other resources in the java server, but because, after all, in the two servers (jvm) in the category, how the transport entity like it?

  • Conventions entity classes
    present example to AgreementApproveForOAthe entity class results returned
  • Receiving a requested code
 @ApiOperation(value = "获取框架协议")
 @PostMapping("/getAgreementApprove")
 public ResponseEntity<AgreementApproveDTO> getAgreementApprove(@RequestParam Integer fcompanyId, @RequestParam Integer fcompanyType, @RequestHeader("fuid") Integer faid) {
      return xxx;
 }
  • Examples of transformation
  public static AgreementApproveForOA getData() throws IOException {
        HttpHeaders headers = new HttpHeaders();
        headers.add("fuid", "2");//设置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);//获取json串
        AgreementResponseEntity agreementResponseEntity;
        //readValue(json字符串,json串对应装配的实体类)
        agreementResponseEntity = mapper.readValue(data.getBody(), AgreementResponseEntity.class); 
        return agreementResponseEntity.getRetEntity();
    }
  • Attach a dependencepom.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>
  • Stepped pit
    • jacksonPackage does not support 2019-01-05 13:25:10this format, the following error
 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 parser

Solution Quote 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;
	}
 
	// 主要还是装饰这个方法
	@Override
	public Date parse(String source) throws ParseException {
 
		Date date = null;
 
		try {
			
			// 先按我的规则来
			date = format1.parse(source);
		} catch (Exception e) {
 
			// 不行,那就按原先的规则吧
			date = dateFormat.parse(source);
		}
 
		return date;
	}
 
	// 这里装饰clone方法的原因是因为clone方法在jackson中也有用到
	@Override
	public Object clone() {
		Object format = dateFormat.clone();
		return new MyDateFormat((DateFormat) format);
	}
}

This can be converted into json corresponding to the entity classes.

END

Guess you like

Origin blog.csdn.net/klli15/article/details/102753982