当我将Servlet设置为SlingAllMethodsServlet时,AEM DefaultGetServlet被调用

AEM DefaultGetServlet is getting called when I set my servlet to a SlingAllMethodsServlet

提问人:jmona789 提问时间:10/19/2023 更新时间:10/19/2023 访问量:32

问:

我有一个接受GET请求的AEM Servlet,我正在尝试将其更新为接受POST,但是当我更新Servlet以接受两者时,它开始调用默认sling servlet。我什至制作了一个非常小的 servlet,它做了一个基本的 Hello World

package com.mysite.microsite.core.servlets;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.OptingServlet;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.osgi.framework.Constants;
import org.apache.sling.api.servlets.HttpConstants;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Component(service = Servlet.class,
    property = {
        "sling.servlet.methods={GET,POST}",
        "sling.servlet.resourceTypes=" + "cq/Page",
        "sling.servlet.selectors=" + "HelloWorld",
        "sling.servlet.extensions=" + "json",
        "service.ranking:Integer=1000"
    }
)
public class HelloWorldServlet extends SlingAllMethodsServlet implements OptingServlet {
    private static final Logger log = LoggerFactory.getLogger(HelloWorldServlet.class);

    /**
     * Add overrides for other SlingAllMethodsServlet here (doHead, doTrace, doPut, doDelete, etc.)
     */

    @Override
    protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.getWriter().write("<html><head></head><body>Hello World!</body></html>");
    }

    @Override
    public final boolean accepts(SlingHttpServletRequest request) {
        /*
         * Add logic which inspects the request which determines if this servlet
         * should handle the request. This will only be executed if the
         * Service Configuration's paths/resourcesTypes/selectors accept the request.
         */
        return true;
    }

}

我在 servlet 解析器 (http://localhost:4502/system/console/servletresolver) 中测试了端点/content/mysite/global/en/path/to/page.HelloWorld.json

即使在这个基本的 servlet 上,当我执行 GET 请求时甚至没有覆盖 doPost,它也会调用 org.apache.sling.servlets.get.impl.DefaultGetServlet。如果我将 servlet 更改为 just use 并将方法更改为 .然后 servlet 工作正常。不知道我在这里做错了什么。SlingSafeMethodsServlet"sling.servlet.methods=GET"

Java 发布 Servlet AEM

评论

0赞 toniedzwiedz 10/23/2023
您确定该方法的实现按预期工作吗?这里省略了它,但这可能是 Servlet 没有被调用的原因。accepts
0赞 ronnyfm 11/9/2023
你找到根本原因了吗?
0赞 jmona789 11/17/2023
我能够通过将sling.servlet.methods分成两行来修复,如下所示:“sling.servlet.methods=”+HttpConstants.METHOD_GET,“sling.servlet.methods=”+HttpConstants.METHOD_POST,

答: 暂无答案