Problem with @Context injection in resource class

It's hard for me to introduce HttpHeaders into my leisure service class.

@Path("/account")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Context
    private HttpHeaders headers;

    @Override
    @POST @Path("/{username}/")
    public User get(@PathParam(value = "username") String username)
        throws UnknownUserException {
        String requestId = headers.getHeaderString("requestId");

        return new User();
    }
}

When I try to start the application, I get the following exception during spring initialization:

Caused by: java.lang.IllegalArgumentException: Can not set javax.ws.rs.core.HttpHeaders field com.acme.service.UserServiceImpl.headers to com.sun.proxy.$Proxy50
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
        at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
        at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
        at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
        at java.lang.reflect.Field.set(Field.java:758)
        at org.apache.cxf.jaxrs.utils.InjectionUtils$1.run(InjectionUtils.java:192)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.cxf.jaxrs.utils.InjectionUtils.injectFieldValue(InjectionUtils.java:188)
        at org.apache.cxf.jaxrs.utils.InjectionUtils.injectContextProxiesAndApplication(InjectionUtils.java:1058)
        at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.injectContexts(JAXRSServerFactoryBean.java:405)
        at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.updateClassResourceProviders(JAXRSServerFactoryBean.java:429)
        at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:162)

A little experimentation shows that I get the same error when trying to enter something. I have an exception mapping unit that has HTTP headers that are inserted into the problem, so I created a custom provider to receive the headers, but I get the same problem injecting this.

I am sure that I must miss something fundamental here.

I know that I could add what I need from the headers as parameters for the method, but I cannot change the interface.

Adding Context to an Operation

    @Override
    @POST @Path("/{username}/")
    public User get(@Context HttpHeaders httpHeaders, @PathParam(value = "username") String username)

Gives me the following error.

java.lang.IllegalArgumentException: object is not an instance of declaring class
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:181)
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:97)

, , , , .

+4
1

(, , ). , spring -, . setHttpHeaders @Context. .

public interface MyService {
    void doStuff();
}

public interface MyServiceInt extends MyService  {
    void setHttpHeaders(HttpHeaders headers);
}

@Path("/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
@Transactional
@Service
public class MyServiceImpl implements MyServiceInt {
    private HttpHeaders httpHeaders;

    @Context
    void setHttpHeaders(HttpHeaders headers) {
        this.httpHeaders = httpHeaders;
    }

    @Override
    @POST @Path("/doStuff")
    public void doStuff() {
    }
}

, - .

+4

Source: https://habr.com/ru/post/1584474/


All Articles