FIQL SearchContext throws NullPointerException when injected with @Context

廉价感情. 提交于 2019-12-12 12:26:31

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!