Dre*_*w13 2 java datetime jax-rs
Intellij 告诉我这updateTime是不正确的参数类型。我不熟悉这个错误和@EnumDateFormat.
    @DELETE
    @Path("groups/{groupId}/samples/{testYear}")
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed({Profile.MP_USER_ROLE})
    public Response deleteGroupSamples (@PathParam("groupId") Long groupId, @PathParam("testYear") Long testYear, @QueryParam("updateTime") @EnumDateFormat( FormatEnum.WITH_HHMMSS ) DateTime updateTime, TreatmentGroupTest sample) throws ValidationException{
        manager.deleteGroupSample( this.getUser(), sample.getTestSampleId(), updateTime );
        ResponseBuilder builder=null;
        builder = Response.ok(  );
        return builder.build();
    }
该错误还表明:
检查参数@PathParam、@QueryParam 等的类型。带注释的参数、字段或属性的类型必须是
是原始类型
有一个接受单个字符串参数的构造函数
有一个名为 valueOf 或 formString 的静态方法,它接受单个 String 参数(例如,参见 Integer.valueOf(String))
拥有 ParamConverterProvider JAX-RS 扩展 SPI 的注册实现,该实现返回能够进行该类型“表单字符串”转换的 ParamConverter 实例
是 List、Set 或 SortedSet,其中 T 满足上述 2、3 或 4。结果集合是只读的
我想这是一个老问题,但是因为LocalDate您必须注册一个实现,ParamConverterProvider该实现返回一个ParamConverter能够LocalDate从String.
如果您检查 javadoc for ParamConverter,您将看到有两种方法:E fromString(String value)和String toString(E value)。此外,如果您查看文档,ParamConverterProvider您会注意到它具有以下方法:<T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation annotations[]).
基本上,您必须创建一个ParamConverter<LocalDate>可以LocalDate从 a创建 a的实现String,然后从注册的 返回该实现的实例ParamConverterProvider。
下面是一个例子:
package com.example;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Optional;
/**
 * The {@link ParamConverterProvider} for {@link java.time} package classes
 * (i.e {@link LocalDate}, {@link LocalTime} and {@link LocalDateTime}).
 */
@Provider
public class Java8TimeParamConverterProvider implements ParamConverterProvider {
    /**
     * Contains a {@link DateTimeFormatter} whose pattern is "yyyy-MM-dd".
     */
    public static final DateTimeFormatter CLASSIC_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    @Override
    public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
        if (rawType == LocalDate.class) {
            //noinspection unchecked
            return (ParamConverter<T>) new Java8LocalDateClassicFormatParamConverter();
        }
        if (rawType == LocalTime.class) {
            return null; // TODO: implement
        }
        if (rawType == LocalDateTime.class) {
            return null; // TODO: implement
        }
        return null;
    }
    /**
     * The {@link ParamConverter} for {@link LocalDate} using the {@link Formatters#CLASSIC_DATE_FORMATTER}.
     */
    private static class Java8LocalDateClassicFormatParamConverter implements ParamConverter<LocalDate> {
        @Override
        public LocalDate fromString(String value) {
            return Optional.ofNullable(value)
                    .map(Formatters.CLASSIC_DATE_FORMATTER::parse)
                    .map(LocalDate::from)
                    .orElse(null);
        }
        @Override
        public String toString(LocalDate value) {
            return Formatters.CLASSIC_DATE_FORMATTER.format(value);
        }
    }
}
您必须注册此提供程序。在你的ResourceConfig你必须包括这个:
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        register(Java8TimeParamConverterProvider.class);
    }
}
或者,如果您正在进行包裹扫描:
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("com.example");
    }
}
(这就是@Provider注释包含在Java8TimeParamConverterProvider类定义中的原因)。请注意,如果您使用的是 spring boot,这将不起作用,因为此时 spring boot 插件创建的最终 jar 存在问题。
另一种注册提供商的方法是通过web.xml. 在这种情况下,您必须包括以下内容:
<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <!-- Include the following init-param if you want to do package scanning -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            com.example
        </param-value>
    </init-param>
    <!-- Or use the following init-param if you want to do class register -->
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>com.example.Java8TimeParamConverterProvider</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
希望这对你有帮助!
| 归档时间: | 
 | 
| 查看次数: | 1711 次 | 
| 最近记录: |