无法调用AEM SlingServlet的doPost方法 - sling resolver默认为GET

时间:2016-11-30 20:55:05

标签: aem sling

我正在使用AEM版本6.2,我需要创建一个连接到后端系统并调用restful服务的servlet。出于安全原因,这必须使用doPost方法而不是doGet。我已经能够在AEM中注册基于Sling的servlet,但是我无法向POST调用POST请求。我已经使用resourceTypes和Paths注册了使用@SlingServlet注释,但是doPost仍然只被调用了doGet。我知道默认调用是来自服务器的doGet,但我已将该方法指定为POST。

package com.dash.dev.servlets.core.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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.jcr.api.SlingRepository;


@SlingServlet(name="A Servlet2",
resourceTypes = "/apps/DashServletApp/components/structure/page",
paths = "/bin/case/enrol2",
methods = "POST", 
metatype=true)

public class AServletByPath extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 7175649210039853979L;
    @Reference
    private SlingRepository repository;

    public void bindRepository(SlingRepository repository){
        this.repository = repository;
    }



protected void doGet(SlingHttpServletRequest request,     SlingHttpServletResponse response) throws ServerException, IOException {
        HttpSession session = request.getSession();
        String sessionID = session.getId();
        System.out.println(sessionID);
          PrintWriter out = response.getWriter();
          out.println(sessionID + "POST Method called");
          out.flush();
          out.close();
    }



protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
        HttpSession session = request.getSession();
        String sessionID = session.getId();
        System.out.println(sessionID);
          PrintWriter out = response.getWriter();
          out.println(sessionID + "POST Method called");
          out.flush();
          out.close();


} catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
           finally
           {
               //Important: Close the connect
               httpClient.getConnectionManager().shutdown();
           }

    }

}

我已阅读Sling documnetation并特别注意:

servlet支持的请求方法。属性值必须是单个String,字符串数组或字符串Vector。此属性仅用于使用sling.servlet.resourceTypes进行注册。如果缺少此属性,则无论servlet实际实现/处理哪些方法,该值都默认为GET和HEAD。

  • 当我看到吊索控制台时,我看到只有GET和HEAD注册。我需要做些什么才能使doPost可用?
谢谢你!

1 个答案:

答案 0 :(得分:4)

正如naeme所暗示的,SlingSafeMethodsServlet应该仅用于" Safe"请求,GETHEADOPTIONS等。默认情况下,此servlet不支持POSTPUTDELETE

引用文档

  

此基类适用于仅读取数据的应用程序。   因此,这个servlet本身不支持POST,PUT和   DELETE方法。这个类的扩展应该覆盖任何   此类的doXXX方法或添加对其他只读的支持   方法而已。

     

希望支持数据修改的应用程序应该   而是使用或扩展也包含的SlingAllMethodsServlet   支持POST,PUT和DELETE方法。后一类应该   也被覆盖以添加对修改数据的HTTP方法的支持。

您可以延长SlingAllMethodsServlet以满足POST次请求。