Reputation: 4334
I am trying to expose some RESTfull webservices on AEM. I have followed the instructions in this blog. Below is my service class. Simple requests like /helloservice/sayhi works perfectly, but the method that take path parameter and query parameters (/withparameter/{userid}
and /query?q=xyz&prod=abc
) return 404 error page. Please help me with this.
I am using AEM 5.6 and Java 7
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import com.foo.bar.service.SampleResource;
@Service
@Component(metatype=true)
@Path("/helloservice")
public class SampleResourceImpl implements SampleResource{
@GET
@Path("/sayhi")
@Produces({MediaType.APPLICATION_JSON})
public String helloWorld(){
return "Hello World";
}
@GET
@Path("/getoperation")
@Produces({MediaType.TEXT_PLAIN})
public String getServiceOperation(){
return "Hello getoperation!";
}
@GET
@Path("/withparameter/{userid}")
@Produces({MediaType.TEXT_PLAIN})
public String getWithParameter(@PathParam("userid") String userid){
return "Path parameter : "+userid;
}
@GET
@Path("/query")
@Produces({MediaType.TEXT_PLAIN})
public String getURLParameters(@QueryParam("q") String q, @QueryParam("prod") String prod){
return "Query params : "+q+", "+prod;
}
}
Any help appreciated, Thanks!
Upvotes: 4
Views: 3121
Reputation: 1242
Cognifide has implemented alternative solution which is called Knot.X. It allows to easly write micro services (and rest APIs). It was written for AEM, but it can be used with any CMS/CRM. Informations, code examples and docs can be found here: http://knotx.io/
Upvotes: 1
Reputation: 1029
This is wrong usage of Sling architecture.
If you want to implement some RESTful service (querying by path, etc) you need to implement specific resource provider.
There is an example. You may need to understand some basic concepts behind it but still its 10 times better in Sling's world than JAX-RS.
Upvotes: 4
Reputation: 63
Some paths and methods of requests are blocked by default on AEM. Go to "Apache Sling Servlet/Script Resolver and Error Handler" on config to allow this /services and go to "Apache Sling Referrer Filter" to remove blocked HTTP methods.
Upvotes: 0
Reputation: 131
I passed the parameters as querystring, then with "UriInfo" from javax.ws.rs.core worked for me:
@GET
@Path("/data")
@Produces({MediaType.APPLICATION_JSON})
public JSONArray getData(@Context UriInfo info) throws JSONException {
String path = info.getQueryParameters().getFirst("p");
String nodename = info.getQueryParameters().getFirst("nn");
Upvotes: 0
Reputation: 6100
There's an ongoing discussion about using JAX-RS in systems based on Apache Sling (which includes AEM) at https://issues.apache.org/jira/browse/SLING-2192 . From that discussion, https://github.com/wcm-io-caravan/caravan-jaxrs looks to me like a good solution to use JAX-RS resources in an OSGi environment. That project's integration tests run on Sling so there's a fair chance that it works on AEM.
Upvotes: 8