Spring REST get object with Date field

2.9k views Asked by At

I'm using Spring RestTemplate with Jackson.

I'm trying to send a list of parameters wrapped inside an object to a controller with a GET request, but I continue to get a 400 error as long as the Date field is present.

This is the object I'm trying to send:

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
}

This is the request I'm sending

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);

and this is the controller that should receive it

@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
    }
}

If I don't send the expiryDate field it works flawlessly.

This is an example of url generated that doesn't work

/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

This instead works

/vpspay/get-payinfo/myid?codeCli=A465&soapPassword=myPass&soapServer=https://111.111.11.11:7654&status=true

I tried to pass the Date as a Long without success.

2

There are 2 answers

1
Nikolay Rusev On BEST ANSWER

it will be more clean way instead of using Date to use timestamps . In simply words they are representation of the date with numbers. see the link : http://www.unixtimestamp.com/ and instead of sending the date will post to server one long number.

1
Butterfly Coder On

I believe that if no converter is specified, Spring will end up calling the deprecated Date constructor, that takes String as an argument to convert the input string to a Date object.

Try passing date in this format '11/12/2012 16:50 PM'