Servlet Returning Null data as json | Community
Skip to main content
Level 3
September 22, 2023
Solved

Servlet Returning Null data as json

  • September 22, 2023
  • 4 replies
  • 1374 views
package com.genc.core.core.models;
import javax.inject.Inject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class DemoServlet
{
@SlingObject
private Resource resource;
@Inject
@Via("resource")
private String subtitle;
@Inject
@Via("resource")
private String description;
public String getUserTitle(){
return resource.getValueMap().get("title",String.class);
}
public String getUserSubTitle(){
return subtitle;
}
public String getUserDescription()
{
return description;
}
}

package com.genc.core.core.servlets;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.genc.core.core.models.DemoServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component(immediate = true,service = Servlet.class)
@SlingServletResourceTypes(
methods = {HttpConstants.METHOD_GET},
resourceTypes = "aem-practice/components/page",
selectors = {"test"},
extensions = {"json","xml"}
)
public class GetServlet extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(GetServlet.class);
@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException
{
DemoServlet demo=req.adaptTo(DemoServlet.class);
if(demo!=null){
Map<String,Object> propertiesValues=new HashMap<>();
propertiesValues.put("Title",demo.getUserTitle());
propertiesValues.put("SubTitle",demo.getUserSubTitle());
propertiesValues.put("Description",demo.getUserDescription());
resp.setContentType("application/json");
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.writeValue(resp.getWriter(),propertiesValues);
}
else{
resp.getWriter().write("Sling Model Not Found");
}
}
}

 Please help me to resolve the issue

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mahedi_Sabuj

You should declare DemoServlet as a Sling Model using the @Model annotation. Here's an example: 

@Model( adaptables = SlingHttpServletRequest.class, adapters = { DemoModel.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL ) public class DemoModel { ..... }

 

4 replies

Mahedi_Sabuj
Community Advisor
Mahedi_SabujCommunity AdvisorAccepted solution
Community Advisor
September 22, 2023

You should declare DemoServlet as a Sling Model using the @Model annotation. Here's an example: 

@Model( adaptables = SlingHttpServletRequest.class, adapters = { DemoModel.class }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL ) public class DemoModel { ..... }

 

Mahedi Sabuj
A_H_M_Imrul
Community Advisor
Community Advisor
September 22, 2023

@vishal33 

 

Are you sure this is the complete servlet? Because I don't see any return if the demo is not null. 

 

Thanks

 

 

  

Vishal33Author
Level 3
October 1, 2023

When I am hitting a servlet in postman it is giving null values.

adivj95
Level 2
September 23, 2023

Try to investigate in error logs , search for error related to this Servlet class in error
try to include debug logs in model file and check logs , I suspect resource.getValueMap().get("title",String.class); this might be giving null

 

BTW you can use Sling model exporter for exporting data as json
https://experienceleague.adobe.com/docs/experience-manager-learn/foundation/development/understand-sling-model-exporter.html?lang=en

 

 

kautuk_sahni
Community Manager
Community Manager
September 25, 2023

@vishal33 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Kautuk Sahni