java - Spring REST 获取带有日期字段的对象

标签 java spring jackson resttemplate

我在 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/

相关文章:

java - SessionFactory未初始化

java - 将 TypeReference 与 Jackson 一起使用

java - 是否可以在 spring 中默认为类型使用自定义序列化器/反序列化器?

java - 为什么我不能在Java中将我自己的类的对象打印到控制台

java - 在排序的链表中查找重复项

java - Spring 数据Jpa : Save method only returning select but not performing insert

java - Jackson/Gson 的良好 REST 设计

java - Spring Boot 错误 404 未找到 Hibernate

java - 为此方法编写单元测试用例返回 RxJava Future

java - 预期有一个匹配的 bean,但发现了 2 个