我找到的解决办法是,自定义参数类型转换器;
public class FormdataModel {
<span class="hljs-meta">@FormParam("name")</span>
<span class="hljs-keyword">private</span> String name;
<span class="hljs-meta">@FormParam("createTime")</span>
<span class="hljs-keyword">private</span> LocalDateTime createTime;
<span class="hljs-meta">@FormParam("price")</span>
<span class="hljs-keyword">private</span> BigDecimal price;
<span class="hljs-meta">@FormParam("file")</span>
<span class="hljs-keyword">private</span> InputPart file;
(1)日期LocalDateTime:
@Provider
@Produces(MediaType.TEXT_PLAIN)
public class LocalDateTimeReader implements MessageBodyReader<LocalDateTime> {
@Override
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return type == LocalDateTime.class;
}
@Override
public LocalDateTime readFrom(Class<LocalDateTime> aClass, Type type, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> multivaluedMap,
InputStream inputStream) throws IOException, WebApplicationException {
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
return LocalDateTime.parse(new String(b), DateTimeFormatter.ofPattern("yyyy-MM-dd HH🇲🇲ss"));
}
}
(2)BigDecimal类型
@Provider
@Produces(MediaType.TEXT_PLAIN)
public class BigDecimalReader implements MessageBodyReader<BigDecimal> {
@Override
public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return type == BigDecimal.class;
}
@Override
public BigDecimal readFrom(Class<BigDecimal> aClass, Type type, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> multivaluedMap,
InputStream inputStream) throws IOException, WebApplicationException {
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
return new BigDecimal(new String(b));
}
}
本文由 GY 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2021/11/19 15:22