我从Java代码发布到另一个站点servlet。它接收数据很好,但由于某种原因,servlet中的resourceResolver不能正常工作。当我使用jQuery发布到servlet时,它工作。 发布到servlet:
String data = "data";
String dataString = "data=" + data;
byte[] dataStringBytes = dataString.getBytes(StandardCharsets.UTF_8);
HttpURLConnection connection = (HttpURLConnection) new URL("http://example:4502/bin/servlet").openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.write(dataStringBytes);
writer.flush();
writer.close();
来自servlet的片段
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
// Only works when posted from jQuery???
ResourceResolver resourceResolver = request.getResource().getResourceResolver();
Session session = resourceResolver.adaptTo(Session.class);
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
}
答案 0 :(得分:1)
确保允许匿名访问该资源。 使用PageManagerFactory API获取PageManager而不是AdaptTo() https://docs.adobe.com/docs/en/cq/5-6-1/javadoc/com/day/cq/wcm/api/PageManagerFactory.html
答案 1 :(得分:0)