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
Solved! Go to Solution.
Views
Replies
Total Likes
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 {
.....
}
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 {
.....
}
Are you sure this is the complete servlet? Because I don't see any return if the demo is not null.
Thanks
When I am hitting a servlet in postman it is giving null values.
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-s...
@Vishal_Jain03 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.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies