【问题标题】:Apache-cxf JAXRSClient custom DateformatApache-cxf JAXRSClient 自定义日期格式
【发布时间】:2017-04-20 15:22:26
【问题描述】:

我正在使用带有代理模式的 apache-cxf-jaxrs。但我无法更改查询参数的日期格式。

有我的界面:

@Path("/api/data")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface DataService {

 @GET
 @Path("/")
 List<DataDto> getData(@JsonFormat(pattern = "yyyyMMdd") @QueryParam("from") Date from);
}

客户端初始化大约:

JacksonJsonProvider jsp = new JacksonJsonProvider();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setDateFormat(new SimpleDateFormat("yyyyMMdd"));       
jsp.setMapper(objectMapper);
DataService  serviceClient = (DataService) 
JAXRSClientFactory.create(baseUrl, serviceClass, Arrays.asList(jsp));

调用以下代码会在我的后端导致 java.time.format.DateTimeParseException,因为日期格式为 DateFormat.DEFAULT 而不是“yyyyMMdd”。

  serviceClient.getData(new Date());

我的带有 SimpleDateFormatter 的 ObjectMapper 从未使用过。我确实忘记了一些东西,但我不知道它是什么。

非常感谢您的帮助。

【问题讨论】:

    标签: java apache jax-rs cxf simpledateformat


    【解决方案1】:

    我明白了。 我必须为 dateformat 注册一个新的 ParamConverterProvider。

    JAXRSClientFactory.create(baseUrl, serviceClass, Arrays.asList(new JacksonJsonProvider(), new MyCustomDataParamConverter()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2021-08-06
      • 2011-03-01
      • 1970-01-01
      相关资源
      最近更新 更多