Custom Sling POST Servlet not working

I have a Servlet that responds to GET requests as expected. The problem is that POST requests are not being handled by my servlet, but rather the SlingPostServlet according to /system/console/requests

. My servlet uses SCR annotations.

@SlingServlet(
  methods = {"POST","GET"}, 
  resourceTypes = {"company/components/pages/thepage"},
  extensions = { "html" }, 
  selectors = { "edit" },
  generateService = true,
  generateComponent = true,
  name = "com.company.services.osgi.package.EditServlet",
  label = "Profile Update Handler"
)

@Properties({
    @Property(name = "service.vendor", value = "Our Company"),
    @Property(name = "service.description", value = "Update Handler") })

public class EditServlet extends SlingAllMethodsServlet {
    ... 
    @Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response){
        // during GET requests this code works!
  }

    @Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response){
  // during POST requests this code is not executing
}

      

I can see in http: // localhost: 4502 / system / console / components that my servlet properties are set and active

component.id = 3463
component.name = com.company.services.osgi.package.EditServlet
sling.servlet.methods = [POST, GET]
sling.servlet.resourceTypes = [company/components/pages/thepage]
sling.servlet.selectors = [edit]
sling.servlet.extensions = [html]

      

I see no errors while installing the package. It seems both REGISTER

07/15/2015 13:05: 24.017 INFO [OsgiInstallerImpl] org.apache.sling.servlets.resolver.internal.SlingServletResolver Registered servletResourceProvider: servlet = com.company.services.osgi.package.EditServlet, paths = [/ libs / foundation / components / primary / company / components / pages / thepage / edit.html.POST.servlet, / libs / foundation / components / primary / company / components / pages / thepage / edit.html.GET.servlet]

I have verified that the page has the correct resource type (if it is not then, I suppose doGet won't work either). What am I doing wrong with POST? Any ideas why doGet works but doPost doesn't?

Update The only way to get this working is to create another servlet for doPost that works for resourceTypes = {"sling/servlet/default" },

, I updated the first one by removing the POST from the methods

@SlingServlet(
  methods = {"POST"}, 
//  resourceTypes = {"company/components/pages/thepage"},
  resourceTypes = {"sling/servlet/default" },
  extensions = { "html" }, 
  selectors =  {"post-servlet"} ,
  generateService = true,
  generateComponent = true,
  name = "com.company.services.osgi.people.UpdateServlet",
  label = "Update Handler",
  metatype=true 
) 

      

+3


source to share


3 answers


we don't see a sample of your POST request, but my guess is that you can post a POST to the page path and the resource type your servlet is registered with is on the jcr: content child resource.



GET requests with html extension to cq: Page resource are redirected internally to the html rendering jcr: content resource, but there are no such requests for POST.

+2


source


Which AEM validation are you using because the 6.x POST request is defined in the CSRF token filter. If the version is 6.x, remove it from the CSRF filter configuration or add a CQ.jquery client reference with a property dependency. This filter will automatically adjust.



0


source


I am facing this problem now. The GET method works fine for me, but not the POST method. followed all the steps mentioned here. when checking the logs, it looks like the mail servlet is running by default instead of the custom servlet. - I linked my custom servlet to a resource type - then created a page using that resource type - and accessed the page using Postman client for GET and POST methods. Any help would be appreciated. thank

0


source







All Articles