我在课堂上添加了一些sun.net软件包时遇到了问题。我添加了此导入:
import sun.net.www.protocol.http.Handler;
当我运行微服务(spring-boot)时,我没有问题,但是当我尝试执行mvn clean install时,出现此错误:
[错误]无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (默认编译)项目坚持执行gg:编译失败[ERROR] /E:/Angelo/Insist/code/insist/insist-api/src/main/java/com/insist/server/service/BlizzardApiService.java:[25,28] 软件包sun.net.www.protocol.http不可见[错误](软件包 在模块java.base中声明了sun.net.www.protocol.http, 不能将其导出到未命名的模块)[错误]
我读到它与一些module-info.java有关,但不确定该怎么做。我正在使用OpenJDK11 btw。
这是我课堂的一部分:
public class SomeApiService {
private String token = null;
private Instant tokenExpiry = null; // Instant when the token will expire
// To allow testing of the URL/Connection
private URLStreamHandler urlStreamHandler = new Handler();
protected String getToken() throws IOException {
if (isTokenInvalid()) {
String encodedCredentials = Base64.getEncoder()
.encodeToString(String.format("%s:%s", insistConfig.getClientID(), insistConfig.getClientSecret())
.getBytes(insistConfig.getEncoding()));
// ------------------------------------------------- Allows testing/mocking of
// the URL connection object
URL url = new URL(insistConfig.getTokenUrl(), "", urlStreamHandler);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", String.format("Basic %s", encodedCredentials));
con.setDoOutput(true);
con.getOutputStream().write("grant_type=client_credentials".getBytes(insistConfig.getEncoding()));
int responseCode = con.getResponseCode();
String response = IOUtils.toString(con.getInputStream(), insistConfig.getEncoding());
// Reads the JSON response and converts it to TokenResponse class or throws an
// exception
TokenResponse tokenResponse = objectMapper.readValue(response, TokenResponse.class);
tokenExpiry = Instant.now().plusSeconds(tokenResponse.getExpires_in());
token = tokenResponse.getAccess_token();
}
return token;
}
}
有什么办法解决此问题或如何替代sun.net.www.protocol.http.Handler?
谢谢