问题
I am trying to implement a simple Rest service using FIQL but my code throws NullPointerException
at the point where I inject the SearchContext
with the @Context
. Here is my code
My service class:
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 class
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;
}
}
I am using maven and I have used the dependency
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-search</artifactId>
<version>2.7.5</version>
</dependency>
According to this CXF-4949 my code (I believe) should work but I still the searchContext
is null after the @Context
Any ideas?
Thanks
回答1:
I managed to solve this out. I was missing the declaration of the SearchContextProvider
in the beans.xml
file. I just added the line
<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider"/>
in the <jaxrs:providers>
tag and now it works fine.
More about FIQL here
Thanks
来源:https://stackoverflow.com/questions/17045353/fiql-searchcontext-throws-nullpointerexception-when-injected-with-context