我想在该方法中使 URL 的端点成为一个变量:
public User fetchUser() throws IOException {
URL url = new URL("https://api.github.com/users/octocat");
InputStreamReader reader = new InputStreamReader(url.openStream());
User user = new Gson().fromJson(reader, User.class);
if (user == null) {
logger.error("Could not return desired output.");
return null;
} else {
logger.info("The output returned.");
return user;
}
}
将其修改为此并不能解决问题(将 'octocat' 更改为 '{endPoint}'):
public User fetchUser(@PathVariable String endPoint) throws IOException {
URL url = new URL("https://api.github.com/users/{endPoint}");
这是我的 RestController 的 GET 方法:
@GetMapping("/user/info/{login}")
public User getUser(@PathVariable String login) throws IOException {
return userService.fetchUser();
}
浏览器返回此消息:
There was an unexpected error (type=Internal Server Error, status=500).
https://api.github.com/users/{endPoint}
另外,如果我将我的 URL 修改为:
URL url = new URL("https://api.github.com/users");
然后响应是这样的:
There was an unexpected error (type=Internal Server Error, status=500).
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
请帮忙。