Java – zoneddatetime as pathvariable in spring rest requestmapping

I have a rest endpoint in my spring application, as shown below

@RequestMapping(value="/customer/device/startDate/{startDate}/endDate/{endDate}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public Page<DeviceInfo> getDeviceListForCustomerBetweenDates(@PathVariable zoneddatetime startDate,@PathVariable zoneddatetime endDate,Pageable pageable) {
    ... code here ...
}

I tried passing path variables as milliseconds and seconds However, I get the following exceptions from two aspects:

"Failed to convert value of type 'java.lang.String' to required type 'java.time.zoneddatetime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.PathVariable java.time.zoneddatetime for value '1446361200'; nested exception is java.time.format.DateTimeParseException: Text '1446361200' Could not be parsed at index 10"

Can someone explain how I pass in (such as seconds or milliseconds) a string, such as 1446361200, and convert it to zoneddatetime?

Or is it the only way to pass as a string and convert it yourself? If so, is there a common way to deal with multiple methods with similar designs?

Solution

The zoneddatetime parameter has a default converter It was created using java 8's datetimeformatter

DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);

As far as you are concerned, this may be any formatstyle or any datetimeformatter, and your example will not be valid Datetimeformatter parses and formats as a date string instead of a timestamp, which is provided by you

You can provide the appropriate custom @ org. For the parameters springframework. format. annotation. Datetimeformat, for example

public Page<DeviceInfo> getDeviceListForCustomerBetweenDates(
    @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) zoneddatetime startDate,@PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) zoneddatetime endDate,Pageable pageable) { ...

Or use the appropriate pattern and the corresponding date string

You will not be able to perform any of the above operations using UNIX timestamps The timestamp converted by the canonical way to zoneddatetime is an appropriate zoneid through instant #ofepochsecond (long)

long startTime = 1446361200L;
zoneddatetime start = Instant.ofEpochSecond(startTime).atZone(ZoneId.systemDefault());
System.out.println(start);

To use it with @ pathvariable, register a custom converter It's like

class zoneddatetimeConverter implements Converter<String,zoneddatetime> {
    private final ZoneId zoneId;

    public zoneddatetimeConverter(ZoneId zoneId) {
        this.zoneId = zoneId;
    }

    @Override
    public zoneddatetime convert(String source) {
        long startTime = Long.parseLong(source);
        return Instant.ofEpochSecond(startTime).atZone(zoneId);
    }
}

And register it in the webmvcconfigurationsupport @ configuration annotation class, overriding addformatters

@Override
protected void addFormatters(FormatterRegistry registry) {
    registry.addConverter(new zoneddatetimeConverter(ZoneId.systemDefault()));
}

Spring MVC will now use this converter to deserialize string path segments into zoneddatetime objects

In spring boot, I think you can declare a @ bean for the corresponding converter, which will automatically register it, but don't accept my words

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>