注入@Context时,FIQL SearchContext抛出NullPointerException [英] FIQL SearchContext throws NullPointerException when injected with @Context

查看:42
本文介绍了注入@Context时,FIQL SearchContext抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的服务等级:

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,并且已经使用了依赖项

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

有什么想法吗?

谢谢

推荐答案

我设法解决了这一问题.我在beans.xml文件中缺少SearchContextProvider的声明.我刚刚添加了行

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"/>

<jaxrs:providers>标记中,现在可以正常工作了.

in the <jaxrs:providers> tag and now it works fine.

有关FIQL的更多信息此处

More about FIQL here

谢谢

这篇关于注入@Context时,FIQL SearchContext抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆