AEM上的RESTful服务

时间:2015-12-23 06:42:17

标签: java rest jax-ws aem

我正在尝试在AEM上公开一些RESTfull Web服务。我已按照此blog中的说明操作。以下是我的服务类。像/ helloservice / sayhi这样的简单请求可以很好地工作,但是采用路径参数和查询参数(/withparameter/{userid}/query?q=xyz&prod=abc)的方法会返回404错误页面。请帮帮我。

我正在使用AEM 5.6和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;
    }

}

感谢任何帮助,谢谢!

5 个答案:

答案 0 :(得分:7)

目前正在讨论在Apache Sling基于https://issues.apache.org/jira/browse/SLING-2192(包括AEM)的系统中使用JAX-RS。从那次讨论中,https://github.com/wcm-io-caravan/caravan-jaxrs看起来像是在OSGi环境中使用JAX-RS资源的好方法。该项目的集成测试在Sling运行,因此它很有可能在AEM上运行。

答案 1 :(得分:3)

这是Sling架构的错误用法。

如果要实现某些RESTful服务(按路径查询等),则需要实现特定的resource provider

There就是一个例子。您可能需要了解它背后的一些基本概念,但在Sling的世界中它仍然比JAX-RS好10倍。

答案 2 :(得分:0)

我将参数作为查询字符串传递,然后使用javax.ws.rs.core中的“UriInfo”为我工作:

@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");

答案 3 :(得分:0)

默认情况下,AEM会阻止某些路径和请求方法。转到配置上的“Apache Sling Servlet /脚本解析器和错误处理程序”以允许此 / services 并转到“Apache Sling Referrer Filter”以删除阻止的HTTP方法。

答案 4 :(得分:0)

Cognifide已经实施了名为Knot.X的替代解决方案。 它允许轻松编写微服务(和其他API)。它是为AEM编写的,但它可以与任何CMS / CRM一起使用。 可以在此处找到信息,代码示例和文档:http://knotx.io/