I am trying to inject resources in my model class. Problem I face when I use annotation #Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
I am getting objects as null, whereas with only Resource.Class I am getting the objects (navigationItems). Below is the snippet of my class. Can you let me know the steps to fix it.
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.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
#Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class Header {
private final Logger logger = LoggerFactory.getLogger(getClass());
#Inject
#Optional
#Named("navitems")
private Resource navigationItems;
List<SiteNavigation> siteNavigationList;
#PostConstruct
protected void init() {
logger.info("In init method of header model.");
siteNavigationList = getSiteNavigationListItems(getNavigationItems());
}
private List<SiteNavigation> getSiteNavigationListItems(final Resource navigationItems, final Resource columnFourItems) {
return null;
}
public Resource getNavigationItems() {
return navigationItems;
}
}
If I remove optional annotation, I am getting below error:
28.09.2018 14:04:39.735 *ERROR* [0:0:0:0:0:0:0:1 [1538123679033] GET /conf/myprj/settings/wcm/templates/homepage/structure.html HTTP/1.1] com.day.cq.wcm.core.impl.WCMDeveloperModeFilter Error during include of SlingRequestPathInfo: path='/conf/myprj/settings/wcm/templates/homepage/structure/jcr:content/root/header', selectorString='null', extension='html', suffix='null'
org.apache.sling.api.SlingException: Cannot get DefaultSlingScript: Identifier com.myprj.core.models.Header cannot be correctly instantiated by the Use API
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:510) [org.apache.sling.scripting.core:2.0.54]
at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) [org.apache.sling.engine:2.6.12]
Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.myprj.core.models.Header
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:679) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:394) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:261) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:126) [org.apache.sling.scripting.sightly.models.provider:1.0.6]
at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:73) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]
... 243 common frames omitted
Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private org.apache.sling.api.resource.Resource com.myprj.core.models.Header.navigationItems
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:684) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
... 247 common frames omitted
Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:581) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:682) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]
... 247 common frames omitted
3 pointers in your code:
While sling9 support multiple adaptables, it is better to adapt from SlingHttpServetlRequest object. It is at higher layer and wraps most other objects.
It is recommended to make your injector more specific than generic #Inject.
Always specify resourceType attribute to associate your model to a specific resource type. Good for exporter of fragments, sling can associate more close and better readability.
This will be my code for your sling model:
#Model(adaptables = SlingHttpServletRequest.class, resourceType = "myprj/components/content/header",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Header {
#ChildResource
private Resource navitems; // Keeping resource name and attribute name identical reduces #Named annotation
This issue resolved with #Via annotation. Below is the snippet I used.
#Inject
#Via("resource")
#Named("navitems")
private Resource navigationItems;
Related
I want to understand which class in Spring framework distinguishes functionality of #Controller, #Service, #Repository annotations. Upon comparing source code of these three annotations understood that only class name is different.
Say, how does spring understand StudentController is only Controller and not Service or Repository?
#Controller
public class StudentController {
}
#Service
public class StudentService {
}
#Repository
public class StudentRepository {
}
Source codes of spring stereotype annotations
Controller.class
package org.springframework.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface Controller {
#AliasFor(
annotation = Component.class
)
String value() default "";
}
Service.class
package org.springframework.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface Service {
#AliasFor(
annotation = Component.class
)
String value() default "";
}
Repository.class
package org.springframework.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Component
public #interface Repository {
#AliasFor(
annotation = Component.class
)
String value() default "";
}
Since source code is same for these annotations, their functionality is distinguished (as each one has different use cases) in framework classes somewhere otherwise framework allows users to use these annotations interchangeable.
I want to understand which class in Spring framework distinguishes
functionality of #Controller, #Service, #Repository annotations. Upon
comparing source code of these three annotations understood that only
class name is different.
The usage is nuanced and often your answers are going to be found by searching for references (e.g. in Eclipse.
For example, #Controller is referenced specifically in RequestMappingHandlerMapping.
Say, how does spring understand StudentController is only Controller
and not Service or Repository?
The plumbing knows what to do with specific annotations. To answer your question directly: it knows StudentController is an #Controller because you have annotated it thus. It is not annotated as #Repository, so it's not a repository.
#Controller itself has a RetentionType.RUNTIME so that Spring can inspect / check for it using reflection.
Finally, note that #Controller (and the other stereotypes you mentioned) are themselves #Components. So a type marked as #Controller is implicitly also a #Component.
Im using wildfly 9.0 to deploy my war file. I have java LocalDateTime, Java Money types defined in my REST GET endpoints.
When i deploy my war file, i get following error[1]. Based on this answer [2] I have written "ParamConverterProvider" implementations for both types.
It was working fine( I haven't seen same issue again till now) and now i get same issue.
Any clue?
[1]
Caused by: java.lang.RuntimeException: Unable to find a constructor that takes a String param or a valueOf() or fromString() method for javax.ws.rs.QueryParam(\"totalMoneyVolumeForPeriod\") on public javax.ws.rs.core.Response com.test.rest.StockEndpoint.getItems(java.lang.Integer,java.lang.Integer,java.lang.String,java.lang.String,java.lang.Long,org.javamoney.moneta.Money,java.util.Set,java.lang.String) for basetype: org.javamoney.moneta.Money"}}}}
[2]
jaxrs could not find my custom (de)serializers for joda.money type
Sample code
package com.test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import javax.money.Monetary;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;
import org.javamoney.moneta.Money;
#Provider
public class MoneyConverterProvider implements ParamConverterProvider {
private final MoneyConverter converter = new MoneyConverter();
#Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (!rawType.equals(Money.class)) return null;
return (ParamConverter<T>) converter;
}
public class MoneyConverter implements ParamConverter<Money> {
public Money fromString(String value) {
if (value == null ||value.isEmpty()) return null; // change this for production
return Money.of(new BigDecimal(value), Monetary.getCurrency("AUD"));
}
public String toString(Money value) {
if (value == null) return "";
return value.toString(); // change this for production
}
}
}
Application claas
package com.test;
import javax.ws.rs.core.Application;
import com.test.autogen*;
import io.swagger.jaxrs.config.BeanConfig;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
#ApplicationPath("/rest")
public class RestApplication extends Application {
public RestApplication() {
BeanConfig beanConfig = new BeanConfig();
//beanConfig.setVersion("1.0");
beanConfig.setSchemes(new String[] { "http" });
beanConfig.setTitle("My API");
beanConfig.setBasePath("/rest");
beanConfig.setResourcePackage("com.test.autogen");
beanConfig.setScan(true);
}
#Override
public Set<Class<?>> getClasses() {
HashSet<Class<?>> set = new HashSet<Class<?>>();
set.add(EmailEndpoint.class);
set.add(StockEndpoint.class);
set.add(io.swagger.jaxrs.listing.ApiListingResource.class);
set.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return set;
}
}
When you are using classpath scanning, JAX-RS components annotated with #Path or #Provider will get picked up and registered. There are a couple way to use classpath scanning. The most common way is to just have an empty Application class annotated with #ApplicationPath
#ApplicationPath("/api")
public class MyApplication extends Application {}
This is enough for a JAX-RS application to be loaded, and to have the application's classpath scanned to components to register.
But, per the specification, once we override any of the Set<Object> getSingletons or Set<Class> getClasses methods of the Application class, and return a non-empty set, this automatically disables classpath scanning, as it is assumed we want to register everything ourselves.
So in previous cases, you were probably just using classpath scanning. In this case, you need to explicitly add the provider to the set of classes in your getClasses method, since you overrode the method to add other component classes.
The code i am trying is showing Null pointer Exception because it is not extracting parameter value from path and i am not getting why? if any one could help me out with this ??
The path value extracted from pathparam i.e is name is extracted as null and why this is happening am not getting please help me out with the same.
I am not getting at all what the problem actually is I tried and I only got that parameter value for name is not getting extracted in path param, I checked out on the different sites still am not able to get the solution. I hope posting over here helps me to find a solution
My resource class is:
package resource;
import javax.websocket.server.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import dao.CustomerDao;
import dao.CustomerDaoImpl;
import entity.Customer;
#Path("/customer")
public class CustomerResource {
CustomerDaoImpl dao = new CustomerDaoImpl();
#GET
#Path("/greet")
public String greet(){
return("Hello World");
}
#GET
#Path("/{name}")
public String getCustomerByName(#PathParam("name") String name){
System.out.println("the name is : "+name);
return dao.getCustomerByName(name).toString();
}
}
and service class is below:
package dao;
import java.util.ArrayList;
import java.util.List;
import entity.Customer;
public class CustomerDaoImpl implements CustomerDao {
static private List<Customer> customerList = new ArrayList<Customer>();
static {
customerList.add(new Customer("John",101,"7757945607"));
customerList.add(new Customer("Joe", 102,"8857833518"));
customerList.add(new Customer("James",103,"8177998482"));
customerList.add(new Customer("Roy",104,"8149038984"));
customerList.add(new Customer("Dev",105,"9503257180"));
}
#Override
public Customer getCustomerByName(String name) {
for (Customer c : customerList) {
if (c.getCustomerName().equalsIgnoreCase(name)) {
return c;
}
}
return null;
}
}
although i am using the correct path
http://localhost:8080/CustomerWebApp/rest/customer/Roy
You are using the wrong PathParam. You should use javax.ws.rs.PathParam.
yes PathParam you have to use provided by jar-rs specification .
you have used wrong one javax.websocket.server.PathParam;
for more Details how to use #PathParam and #path annotation plz refer:http://entityclass.in/rest/jaxRsAnnotationPath.htm
I'm doing some performance testing, and I want to be able to call a resource method without going through the network. I already have a framework for generating URLs, and I'd like to be able to reuse it.
For example, given the URL: www.example.com:8080/resource/method, I want to get a reference to the resource method that it calls, so that I can run it without making a network-level HTTP request. I.e., in the example below, I want to use the URL "www.frimastudio.com:8080/time" to get a reference to the method getServerTime() that I can then call directly.
Does Jersey (or something else?) provide a way to do this, or do I have to import the specific Resource class I want to call, instantiate it, etc.? Thanks in advance!
Yes jersey is RESTful API that allow routes configuration (only with annotations)
Example :
package com.frimastudio.webservice.controller.route;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.joda.time.DateTime;
import com.frimastudio.webservice.controller.representation.Time;
#Path("/time")
#Produces(MediaType.APPLICATION_JSON)
public class TimeResource
{
public TimeResource()
{
}
#GET
public Time getServerDate()
{
return new Time(new DateTime());
}
}
with Time being a Jackson representation :
package com.frimastudio.webservice.controller.representation;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.DateTime;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Time
{
#NotEmpty
#JsonProperty
private String date;
public Time()
{
// Jackson deserialization
}
public Time(String date)
{
super();
this.date = date;
}
public Time(DateTime date)
{
super();
this.date = date.toString();
}
}
This doesn't seem to be possible, based on looking at the Jersey code. The lookup is performed by HttpMethodRule.Matcher, which is a private class used only to implement HttpMethodRule.accept.
Seems to me everything in accept up to if (s == MatchStatus.MATCH) { could be pulled into its own method and exposed to the user.
I would like to have the following returned to the browser (view source)
<content>
<![CDATA[Please show this inside a unescaped CDATA tag]]>
</content>
But I acutally get
<content>
Please show this inside a unescaped CDATA tag
</content>
If, I change the value of content to be
< ;![CDATA[Please show this inside a unescaped CDATA tag]]> ;
, the less than and the greater than for the tag are escaped.
Wondering how to achieve what I wanted????
Here is my code
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/myRequest")
public class MyRestClass {
#GET
#Path("{myPathNumber}")
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Object doInquiry(#PathParam("myPathNumber") String myPathNumber) {
try {
return new MyObject();
} catch (Exception e) {
return "exception " + e.getMessage();
}
}
}
package org.openengine.wink;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
#XmlRootElement
public class MyObject implements Serializable {
#XmlElement
#XmlCDATA
private String content = "Please show this inside a unescaped CDATA tag";
}
in package org.openengine.wink I have a file, jaxb.properties, with the following content
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
CLASSPATH
My best guess is that EclipseLink JAXB (MOXy) is not correctly configured on your classpath, and the JAXB RI is being used as the JAXB (JSR-222) provider in your environment.
METADATA
The EclipseLink JAXB (MOXy) metadata you have provided appears to be correct. This can be verified with the following standalone demo code.
MyObject
By default JAXB (JSR-222) implementations look for metadata on the property (getter/setter). Since you have annotated the field I would recommend using the #XmlAccessorType(XmlAccessType.FIELD annotation (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
package org.openengine.wink;
import java.io.Serializable;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class MyObject implements Serializable {
#XmlElement
#XmlCDATA
private String content = "Please show this inside a unescaped CDATA tag";
}
jaxb.properties
To specify MOXy as your JAXB provider you need to have the EclipseLink binaries on your classpath and have a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package org.openengine.wink;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyObject.class);
MyObject myObject = new MyObject();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(myObject, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8"?>
<myObject>
<content><![CDATA[Please show this inside a unescaped CDATA tag]]></content>
</myObject>
For More Information
http://blog.bdoughan.com/2010/07/cdata-cdata-run-run-data-run.html