我在 Jackson 中使用 Spring RestTemplate。
我试图通过 GET 请求将包含在对象内的参数列表发送到 Controller ,但只要 Date 字段存在,我就会继续收到 400 错误。
这是我要发送的对象:
public class UserPmVpxpServiceDTO implements GenericDTO {
private static final long serialVersionUID = 1L;
private String codeCli;
private String soapPassword;
private Date expiryDate;
private String soapServer;
private Boolean status;
public UserPmVpxpServiceDTO() {
}
@JsonCreator
public UserPmVpxpServiceDTO(@JsonProperty("codeCli") final String codeCli,
@JsonProperty("soapPassword") final String soapPassword,
@JsonProperty("expiryDate") final Date expiryDate,
@JsonProperty("soapServer") final String soapServer,
@JsonProperty("status") final Boolean status) {
this.codeCli = codeCli;
this.soapPassword = soapPassword;
this.expiryDate = expiryDate;
this.soapServer = soapServer;
this.status = status;
}
// getters and setters
}
这是我要发送的请求
final UriComponentsBuilder path = UriComponentsBuilder.fromUriString(PMPCG_URL).path(UrlMap.PCG_GET_PAY_INFO);
path.queryParam("codeCli", userPmVpxpServiceDTO.getCodeCli());
path.queryParam("soapPassword", userPmVpxpServiceDTO.getSoapPassword());
path.queryParam("expiryDate", userPmVpxpServiceDTO.getExpiryDate());
path.queryParam("soapServer", userPmVpxpServiceDTO.getSoapServer());
path.queryParam("status", userPmVpxpServiceDTO.getStatus());
final URI uriPcg = path.buildAndExpand(id).toUri();
return restTemplate.getForObject(uriPcg.toString(), PayInfoDTO.class, userPmVpxpServiceDTO);
这是应该接收它的 Controller
@RestController
public class VpsPayController {
@RequestMapping(value = UrlMap.PCG_GET_PAY_INFO, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public PayInfoDTO getPayInfo(final UserPmVpxpServiceDTO userPmVpxpServiceDTO, @PathVariable final String id) throws RemoteException, ServiceException {
// my code
}
}
如果我不发送 expiryDate 字段,它将完美运行。
这是生成的 url 不起作用的示例
/vpspay/get-payinfo/myid?codeCli=A465&soapPassword=myPass&expiryDate=Tue%2520Dec%252031%252000:00:00%2520CET%25202999&soapServer=https://111.111.11.11:7654&status=true
这反而有效
/vpspay/get-payinfo/myid?codeCli=A465&soapPassword=myPass&soapServer=https://111.111.11.11:7654&status=true
我试图将日期作为 Long
传递,但没有成功。
最佳答案
它会比使用 Date 来使用 timestamps 更干净。简而言之,它们是用数字表示日期。 查看链接:http://www.unixtimestamp.com/而不是发送日期,而是将一个长号码发布到服务器。
关于java - Spring REST 获取带有日期字段的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30775342/