我想通过API调用由Spring-Boot构建的本地REST服务器,该服务器与mongodb进行交互。我已经检查了一些关于该主题的帖子,但是我的问题似乎有些不同。
以下是一些相关的代码片段:
protected static CoreEntity[] sendGET(CoreEntity[] resultList, String path) throws IOException {
path = String.join("%20", path.split(" "));
return handleResponse(resultList, getConnection(path, "Get"));
}
private static HttpURLConnection getConnection(String path, String requestMethod) throws IOException {
URL url = new URL(REQUEST_URL + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("accept", "application/json");
connection.setConnectTimeout(50000);
connection.setReadTimeout(50000);
connection.setRequestMethod(requestMethod);
initializeGSON();
return connection;
}
private static CoreEntity[] handleResponse(CoreEntity[] resultList, HttpURLConnection connection) {
try {
final int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) { // Success
try (InputStreamReader reader = new InputStreamReader(connection.getInputStream()); BufferedReader in = new BufferedReader(reader)) {
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) { response.append(inputLine); }
reader.close();
in.close();
JSONArray jsonArray = getJSONAsArray(response.toString());
resultList = (CoreEntity[]) Array.newInstance(resultList.getClass().getComponentType(), jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++)
resultList[i] = (CoreEntity) GSON.fromJson(jsonArray.get(i).toString(), resultList.getClass().getComponentType());
} catch (IOException | JSONException e) { e.printStackTrace(); }
} else {
System.out.println("\nRequest failed with error code: " + status);
}
connection.disconnect();
return resultList;
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
http://www.google.com或任何其他主页的响应成功,状态码为200。但是,一旦我调用API,就会收到状态码为404的错误。奇怪的是,当我使用Postman或浏览器时,一切正常。因此,当我通过邮递员对以下方法(http://localhost:8080/pets/3)进行get请求时,可以看到打印输出并从mongodb获取数据,但对于上面的代码则没有。对于上面的代码,服务器端没有任何反应,没有输出,没有异常,什么都没有。
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<T> getById(@PathVariable final long id) {
System.out.println("TEST ===> " + id);
T entity = getService().getById(id);
return entity == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(entity);
}
似乎我的应用程序无法找到该API,但是我已经验证了URL正确,这就是为什么我不理解错误代码404的原因。
我也阅读了软件包的可见性,但是我的结构看起来像下面,这就是为什么我不认为这是原因。
Package Structure (Don't be confused from name Aerospike)
我现在为此花费了太多时间,我真的很想得到帮助,希望您能为我提供帮助,或者至少为我指明正确的方向。
编辑
这里是整个RestController:
@Controller
public abstract class CoreController<S extends CoreService<T>, T extends CoreEntity> {
public static final String SERVER = "http://localhost", PORT = ":8080",
CORE_API = SERVER + PORT + "/"; // controller/v2/
public static final String ID = "id";
private String api;
public CoreController() { }
public CoreController(final String api) { this.api = CORE_API + api; }
@RequestMapping(value = "/{" + ID + "}", method = RequestMethod.GET)
public ResponseEntity<T> getById(@PathVariable final long id) {
System.out.println("TEST ===> " + id);
T entity = getService().getById(id);
return entity == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(entity);
}
public abstract S getService();
}
@Controller
@RequestMapping(value = "pets/")
public class PetController extends CoreController<PetService, Pet> {
@Autowired
protected PetService service;
public static final String API = "pets";
public PetController() { super(API); }
public PetService getService() { return service; }
}
[这里有证据表明,弹簧靴正在监听8080,邮递员也在8080端口上工作。
我认为您应该在开始时错过反斜杠,并且在暴露值的末尾重复,所以它在控制器中寻找pets // {id}更改为value = {“ / pets”}无论如何,当启动服务s时,您应该在日志中看到暴露的uri的