我正在尝试使用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);
}
}
我的书课
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>
根据this CXF-4949 ,我的代码(我相信)应该可以工作,但searchContext
在@Context
有任何想法吗?
谢谢