CXF + Spring:未找到根资源匹配请求路径

时间:2016-07-19 13:51:40

标签: spring web-services rest cxf

尝试使用CXF和Spring启动并运行REST服务,我遇到了这个问题。其他一切看起来很好,因为我可以访问我的/ services /页面并查看REST服务下列出的MyService。我可以很好地查看服务的WADL,我的方法出现在那里。当我尝试访问它时,会发生异常。

WARNING: No root resource matching request path /testApp/services/MyService/goTest has been found, Relative Path: /goTest. Please enable FINE/TRACE log level for more details.
Jul 19, 2016 4:30:50 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: javax.ws.rs.NotFoundException: HTTP 404 Not Found

我的服务界面

@Path("/MyService")
@Service
public interface MyService {

    @GET
    @Path("/goTest")
    @Produces(MediaType.APPLICATION_JSON)
    public Response testService();
}

服务实施:

public class MyServiceImpl implements MyService {

public Response testService() {
        // Code

        return Response.status(200).entity(gson.toJson(json)).build();
    }
}

配置类:

 @Configuration
 public class WebServiceSpringConfig {

    @Bean(name=Bus.DEFAULT_BUS_ID)
        public SpringBus springBus() {
            return new SpringBus();
        }


        @Bean
        public Server MyService() {

            JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
            endpoint.setBus(springBus());
            endpoint.setAddress("/MyService");
            endpoint.setServiceBean(new MyServiceImpl());
            return endpoint.create();
        }
    }

Spring config和CXF servlet在web.xml中声明

我使用Spring 4.2.0和CXF 3.1.4

1 个答案:

答案 0 :(得分:4)

您的网址goTest可以访问,如下所示。

http://<ip-add>:<port>/<web-root>/services/MyService/MyService/goTest

简而言之,再次添加MyService,因为您已将MyService设置为setAddress,同时也设置为@Path

/testApp/services/MyService/MyService/goTest