我是Maven,Spring和CXF的新手,目前正在努力寻找一个小小的“Hello World'我可以使用REST服务的地方。我花了一天时间在这上面,但我仍然遇到了可怕的事情。#34;没有找到任何服务。"我在Eclipse内的Tomcat上运行项目时出错。下面我发布了几个文件中的重要代码。我希望有人可以告诉我我做错了什么:
的web.xml:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
beans.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="bookserviceclass" class="org.ahmad.restTest1.restServices.BookService" />
<jaxrs:server id="bookservice" address="/">
<jaxrs:serviceBeans>
<ref bean="bookserviceclass" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
BookService.java:
package org.ahmad.restTest1.restServices;
import org.ahmad.restTest1.vo.BookVO;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
@Path("/")
public class BookService {
private static HashMap < BookVO, BookVO > hashMap;
@GET
@Path("/randomMsg/{name}")
@Produces({
MediaType.APPLICATION_JSON
})
public Response getSomeMessage(@PathParam("name") String name) {
return Response.ok(name + "123").build();
}
}
答案 0 :(得分:3)
您有一些配置错误。
在web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
删除beans.xml
中的这一行。最新版本的CXF中不需要它们
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
此后,服务器将可执行
http://localhost:8080/yourdeploydir/randomMsg/hello