简体   繁体   English

JAX-RS方法没有路径或HTTP方法注释

[英]JAX-RS method has no Path or HTTP method annotations

I am implementation another implementation for existing interface using Spring and Apache CXF. 我正在使用Spring和Apache CXF为现有接口实现另一种实现。 When tomcat is starting up, it show below error message : 当tomcat启动时,它显示以下错误消息:

Method getSomething in ModuleInterface has no JAX-RS Path or HTTP Method annotations ModuleInterface中的方法getSomething没有JAX-RS路径或HTTP方法注释

And, both endpoints are returning 404. 并且,两个端点都返回404。

I am not sure what am I missing. 我不确定我想念的是什么。 Any idea anyone? 有人知道吗?

public interface ModuleInterface {
   public Response getSomething(@Valid RequestObj obj);
}

-- -

@Service
@Path("/foo")
public class FooClass implements moduleInterface {
public Response getSomething(@Valid Request obj){
// code
}
}

-- -

@Service
@Path("/new/foo")
public class FooV2Class implements moduleInterface {
public Response getSomething(@Valid Request obj){
// code
}
}

-- -

@Configuration
@ImportResource({"classpath:/META-INF/cxf/cxf-servlet.xml", "classpath:/META-INF/cxf/cxf.xml"})
public class APIConfig {
@Autowired @Lazy ModuleInterface moduleInterface;

@Bean
public Server initCxfServer(){
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setServiceBeanObjects(getJaxRsResources());
    sf.setProviders(Arrays.asList(
            new JacksonJaxbJsonProvider()
    ));

    return sf.create();
}

private Object[] getJaxRsResources() {
    return new Object[]{
        moduleInterface
    };
}

private HashMap getExtMaps() {
    return new HashMap<String,String>(){{
        put("json","application/json;charset=utf-8");
        put("xml","application/xml;charset=utf-8");
        put("wadl","application/vnd.sun.wadl+xml");
        put("desc","application/vnd.sun.desc+json;charset=utf-8");
       }};
}

This may happend if you annonate the arguments in boths methods, in the interface and in the implementantion class, with @Valid (or even other annotation like @Context ). 如果您使用@Valid (或其他类似@Context注解)在两个方法中,在接口中以及在Implementantion类中注释参数,则可能会发生这种情况。

Keep the annotation only on the arguments of the method that you annotated with the @Path (either in the interface, either in the implementation class) and it should work. 将注释仅保留在使用@Path注释的方法的参数上(在接口中,在实现类中),并且注释应该起作用。

You need to let CXF know about your services and/or configure CXF. 您需要让CXF知道您的服务和/或配置CXF。

See: http://cxf.apache.org/docs/jaxrs-services-configuration.html#JAXRSServicesConfiguration-ConfiguringJAX-RSservicesincontainerwithSpringconfigurationfile for CXF JAX-RS configuration with Spring. 有关使用Spring进行CXF JAX-RS配置的信息,请参见: http ://cxf.apache.org/docs/jaxrs-services-configuration.html#JAXRSServicesConfiguration-ConfiguringJAX-RSservicesincontainerwithSpringconfigurationfile。

EDIT 编辑

If you have multiple instances, which one do you expect to be loaded with following line ? 如果您有多个实例,您希望在下一行中加载哪个实例?

@Autowired @Lazy ModuleInterface moduleInterface;

Shouldn't an array achieve what you want ? 数组不应该实现您想要的吗? Like : 喜欢 :

@Autowired @Lazy ModuleInterface[] moduleInterfaces;

Your classes are scanned for methods which can receive requests ( Resource methods ). 扫描您的类以查找可以接收请求的Resource methodsResource methods )。 getSomething is a candidate since it has the parameter annotation @Valid but it does not qualify as resource method since it is missing a JAX-RS Path or HTTP Method annotation. getSomething是候选对象,因为它具有参数注释@Valid但由于缺少JAX-RS路径或HTTP方法注释,因此不符合资源方法的要求。

For instance adding a @GET annotation will do it (if you want to receive HTTP GET requests via this method). 例如,添加@GET批注即可(如果您想通过此方法接收HTTP GET请求)。

public interface ModuleInterface {
    @GET
    public Response getSomething(@Valid RequestObj obj);
}

I had the same warning. 我也有同样的警告。 In my case I used CXF 3.1.3 and I missed to add the spring feature within karaf. 就我而言,我使用的是CXF 3.1.3,但是我错过了在karaf中添加spring功能。

For more details see Migration Guide to CXF 3.1.x 有关更多详细信息,请参阅 CXF 3.1.x 迁移指南》

Adding name of the bean and using qualifier annotation 添加bean的名称并使用限定符注释

@Service("foo")
@Path("/foo")
public class FooClass implements ModuleInterface {
public Response getSomething(@Valid Request obj){
// code
}
}
--

@Service("foov2")
@Path("/new/foo")
public class FooV2Class implements moduleInterface {
public Response getSomething(@Valid Request obj){
// code
}
}

@Component
public class Service {
@Autowired
@Qualifier("foo")
ModuleInterface moduleInterface
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM