注入@Context时,FIQL SearchContext会抛出NullPointerException

时间:2013-06-11 13:29:14

标签: cxf jax-rs

我正在尝试使用 FIQL 来实现简单的Rest服务,但我的代码会在NullPointerException注入SearchContext时抛出@Context。这是我的代码

我的服务类:

import java.util.*;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchContext;

@Path("/search")
public class Books {

    private List<Book> theBooks = new LinkedList<Book>();

    @Path("/book")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public List<Book> getBook(@Context SearchContext searchContext) {

        theBooks.add(new Book("1", "nick1"));
        theBooks.add(new Book("2", "nick2"));
        theBooks.add(new Book("3", "nick3"));
        theBooks.add(new Book("4", "nick4"));
        theBooks.add(new Book("5", "nick5"));

        SearchCondition<Book> condition = searchContext
                .getCondition(Book.class);

        return condition.findAll(theBooks);
    }
}

My Book课程

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book {

    private String id;
    private String author;

    public Book(){  }
    public Book(String id, String ownerinfo) {
        this.id = id;
        this.author = ownerinfo;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getOwnerinfo() {
        return author;
    }
    public void setOwnerinfo(String ownerinfo) {
        this.author = ownerinfo;
    } 
}

我正在使用maven,我使用了依赖

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-extension-search</artifactId>
    <version>2.7.5</version>
</dependency>

根据此CXF-4949我的代码(我相信)应该有效,但searchContext

之后@Context仍为空

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:5)

我设法解决了这个问题。我错过了SearchContextProvider文件中beans.xml的声明。我刚添加了一行

<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider"/>
<{1>}代码中的

现在可以正常使用。

有关FIQL here

的更多信息

由于