带有cxf-rs和spring集成的HTTP-404

我是Web服务开发的新手 . 尝试使用带有spring集成的apache-cxf构建基本的restfull webservice . 但是当我尝试访问其余资源uri时,http-404即将到来 . 以下是可能有问题的文件:

Web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    metadata-complete="true">

    <display-name>RestDemo</display-name>
    <description>RestDemo</description>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>springrest-servlet.xml</param-value>
    </context-param>

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

<servlet>
    <servlet-name>springrest</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springrest</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

springrest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<context:component-scan base-package="com.sohel.resource" />

MessageResource.java

@Path("/messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)


public class MessageResource {

    MessageService messageService = new MessageService();

    @GET
public List<Message> getMessages(@BeanParam MessageFilterBean filterBean) {

    if (filterBean.getYear() > 0) {
        return messageService.getAllMessagesForYear(filterBean.getYear());
    }
    if (filterBean.getStart() >= 0 && filterBean.getSize() > 0) {
        return messageService.getAllMessagesPaginated(filterBean.getStart(), filterBean.getSize());
    }
    return messageService.getAllMessages();
}

我试图访问的uri http://localhost:8080/CopyOfGood/webapi/messages

回答(1)

3 years ago

您必须在 springrest-servlet.xml 中包含jaxrs server和serviceBeans配置 . 查看apache cxf docs中的 beans.xml 文件,其中包含有关如何配置服务器和serviceBeans的信息 .