gpt4 book ai didi

java - 为枚举实现 MessageBodyReader

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:25 25 4
gpt4 key购买 nike

我正在尝试实现一个新的 MessageBodyReader/Writer 组合,但阅读器部分遇到问题。鉴于以下声明

public class Jsr367EnumProvider<T extends Enum> implements
MessageBodyReader<T>,
MessageBodyWriter<T>

可以工作,但有一条警告说 Enum是生的。因此,为了修复它,我尝试输入 Enum<?>

但是这会在我的 readFrom 中产生错误方法

@Override
public T readFrom(final Class<T> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, String> httpHeaders,
final InputStream inputStream) throws IOException,
WebApplicationException {

// TODO support other encodings using the HTTP Header
try (Scanner s = new Scanner(inputStream).useDelimiter("\\A")) {
return (T) Enum.valueOf(type, s.next());
}

}

这也警告了未经检查的 Actor 。有没有办法解决这个问题而不诉诸 @SupressWarnings

最佳答案

不,没有。回想一下您尝试转换的方法的签名:

Enum.valueOf(Class<T> enumType, String name)

严格来说,您提供的代码,您无法摆脱警告。因此,抑制它,但遵循良好的做法,例如《Effective Java 2nd Edition: Item 24: Eliminate unchecked warnings:》中所规定的内容:

  • Eliminate every unchecked warning that you can.
  • If you can't eliminate a warning, and you can prove that the code that provoked the warning is typesafe, then (and only then) suppress the warning with @SuppressWarning("unchecked") annotation.
  • Always use the SuppressWarning annotation on the smallest scope possible.
  • Every time you use an @SuppressWarning("unchecked") annotation, add a comment saying why it's safe to do so.

关于java - 为枚举实现 MessageBodyReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32042126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com