【问题标题】:Spring REST get object with Date fieldSpring REST获取带有日期字段的对象
【发布时间】:2015-06-11 08:21:02
【问题描述】:

我在 Jackson 中使用 Spring RestTemplate。

我正在尝试使用 GET 请求将包装在对象中的参数列表发送到控制器,但只要存在 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);

这是应该接收它的控制器

@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 传递,但没有成功。

【问题讨论】:

    标签: java spring jackson resttemplate


    【解决方案1】:

    这将是更干净的方式,而不是使用 Date 来使用 timestamps 。简而言之,它们是用数字表示日期。 请参阅链接:http://www.unixtimestamp.com/,而不是将日期发送到服务器,而不是发送一个长号码。

    【讨论】:

    • 这样就成功了,谢谢!尽管如此,我希望它能够像 POST 请求一样适用于 GET 请求。
    【解决方案2】:

    我相信如果没有指定转换器,Spring 最终会调用已弃用的 Date 构造函数,该构造函数将 String 作为参数将输入字符串转换为 Date 对象。

    尝试以“11/12/2012 16:50 PM”这种格式传递日期

    【讨论】:

    • 我在 Date getter 中添加了@JsonFormat(shape= Shape.STRING, pattern="yyyy/MM/dd HH:mm"),并调用了/vpspay/get-payinfo/myid?codeCli=A465&soapPassword=myPass&expiryDate=2999/12/31%252000:00&soapServer=https://111.111.11.11:7654&status=true,但结果是一样的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2020-09-03
    • 2017-06-09
    • 1970-01-01
    • 2021-03-17
    • 2017-07-25
    • 2023-04-05
    相关资源
    最近更新 更多