0

I would like wrap UriInfo into my custom Query object and make it available in other Spring components. I was able to do so with HK2 like this

package test.service;

@Path("/test")
public class TestResource {

    @Inject
    private Query query;

    @GET
    public String test() {
        return "found: " + query.toString();
    }
}

with proper factory

package test.utils;

public class QueryFactory implements Factory<Query> {

    @Inject
    private UriInfo uriInfo;

    @Override
    public Query provide() {
        Query query = new Query();
        Iterator<String> it = uriInfo.getQueryParameters().keySet().iterator();

        if(it.hasNext()) {
            String key = it.next();
            List<String> valueList = uriInfo.getQueryParameters().get(key);
            String value;
            if(valueList.isEmpty())
                value = "no value";
            else
                value = valueList.get(0);

            query.setKey(key);
            query.setValue(value);
        } else {
            query.setKey("no query");
        }
        return query;
    }

    @Override
    public void dispose(Query query) {
    }
}

Now I would like to switch to spring. I tried by adding configuration

package test.config

@Configuration
@ComponentScan("test")
public class SpringConfiguration {

    @Autowired
    QueryFactory queryFactory;

    @Bean
    public Query query() throws Exception {
        return queryFactory.getObject();
    }
}

updating web.xml

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>test.config.SpringConfiguration</param-value>
</context-param>

changing TestResource

package test.service;

@Path("/test")
@Component
public class TestResource {

    @Autowired
    private Query query;

    @GET
    public String test() {
        return "found: " + query.toString();
    }
}

and adding annotation to QueryFactory

@Component
@Scope("request")
public class QueryFactory implements FactoryBean<Query>

I understand that RequestContextListener is required to work with request scope. However, when I run it I get exception that tells me

`java.lang.IllegalStateException: No thread-bound request found:` 

Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of

DispatcherServlet/DispatcherPortlet:

In this case, use RequestContextListener or RequestContextFilter to expose the current request.

What is ridiculous since I already added RequestContextListener to web.xml.

If I run without ContextLoaderListener set, then I get NullPointerException in TestResource because query is null.

I cannot see what is wrong. Can you point it out to me how should be this configuration done properly?

Maheshwar Ligade
  • 6,171
  • 3
  • 35
  • 53
lord.didger
  • 1,205
  • 1
  • 14
  • 30

1 Answers1

2

The problem is that you're telling Spring that QueryFactory has a request scope

@Component
@Scope("request")
public class QueryFactory

So, this objec only exists in the middle of a web request. But you're trying to use it outside a web request.

@Autowired
QueryFactory queryFactory;

You can see it in the Exception

Are you referring to request attributes outside of an actual web request

reos
  • 7,402
  • 3
  • 23
  • 33
  • I get exception because there is no UriInfo. I want this object from request. UriInfo is not my class but something available for web applications. – lord.didger Dec 16 '15 at 15:54
  • You're injecting QueryFactory into the SpringConfiguration class. This configuration class init at the begining of your app and it's not part of a web request. – reos Dec 16 '15 at 16:08