带有泛型的静态方法中的 Jackson ObjectMapper
Jackson ObjectMapper in Static Method with Generics
我有一个静态方法,旨在使用 ObjectMapper 读取 JSON 并解析为 Class(在运行时指定)。我想 return 一个 'N' 类型的对象,但是我在使用泛型时遇到错误。
我怎样才能让下面的代码完成这个?
public static <N, T extends AbstractRESTApplication> N GET_PAYLOAD( T app, String urlString, REQUEST_TYPE requestType) throws JsonProcessingException, MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException {
HttpsURLConnection con = null;
try {
RSSFeedParser.disableCertificateValidation();
URL url = new URL(urlString);
con = (HttpsURLConnection) url.openConnection();
String encoding = Base64.getEncoder().encodeToString((app.getUser() + ":" + app.getPassword()).getBytes("UTF-8"));
con.setRequestProperty("Authorization", String.format("Basic %s", encoding));
//con.setDoOutput(true);//only used for writing to.
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestMethod(requestType.toString());
con.setRequestProperty("User-Agent", "Java client");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
// wr.write(val);
StringBuilder content;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String line;
content = new StringBuilder();
while ((line = in.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
System.out.println(Class.class.getName() + ".GET_PAYLOAD= " + content);
//Map Content to Class.
ObjectMapper om = new ObjectMapper();
return om.readValue(content.toString(),N);//Doesn't Like N type. How do i fix?
}
} finally {
con.disconnect();
}
}
Java 泛型是使用 "type erasure" 实现的。这意味着编译器可以检查类型安全并在 运行 时间删除类型。
所以你不能那样使用你的类型变量 ("N")。您必须将实际的 class 作为参数传递:
public static <N, T extends AbstractRESTApplication> N GET_PAYLOAD( T app,
String urlString, REQUEST_TYPE requestType,
Class<N> nClass) throws ... {
return om.readValue(content.toString(), nClass);
我有一个静态方法,旨在使用 ObjectMapper 读取 JSON 并解析为 Class(在运行时指定)。我想 return 一个 'N' 类型的对象,但是我在使用泛型时遇到错误。
我怎样才能让下面的代码完成这个?
public static <N, T extends AbstractRESTApplication> N GET_PAYLOAD( T app, String urlString, REQUEST_TYPE requestType) throws JsonProcessingException, MalformedURLException, IOException, NoSuchAlgorithmException, KeyManagementException {
HttpsURLConnection con = null;
try {
RSSFeedParser.disableCertificateValidation();
URL url = new URL(urlString);
con = (HttpsURLConnection) url.openConnection();
String encoding = Base64.getEncoder().encodeToString((app.getUser() + ":" + app.getPassword()).getBytes("UTF-8"));
con.setRequestProperty("Authorization", String.format("Basic %s", encoding));
//con.setDoOutput(true);//only used for writing to.
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestMethod(requestType.toString());
con.setRequestProperty("User-Agent", "Java client");
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
// wr.write(val);
StringBuilder content;
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))) {
String line;
content = new StringBuilder();
while ((line = in.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
System.out.println(Class.class.getName() + ".GET_PAYLOAD= " + content);
//Map Content to Class.
ObjectMapper om = new ObjectMapper();
return om.readValue(content.toString(),N);//Doesn't Like N type. How do i fix?
}
} finally {
con.disconnect();
}
}
Java 泛型是使用 "type erasure" 实现的。这意味着编译器可以检查类型安全并在 运行 时间删除类型。
所以你不能那样使用你的类型变量 ("N")。您必须将实际的 class 作为参数传递:
public static <N, T extends AbstractRESTApplication> N GET_PAYLOAD( T app,
String urlString, REQUEST_TYPE requestType,
Class<N> nClass) throws ... {
return om.readValue(content.toString(), nClass);