gpt4 book ai didi

spring - 使用 Maven 和 tomcat 的 CXF Rest

转载 作者:行者123 更新时间:2023-11-28 23:31:21 25 4
gpt4 key购买 nike

我尝试在 tomcat 中启动并运行我的第一个休息应用程序。我在构建过程中使用 maven。构建工作没有任何错误。

Tomcat 提取 rest.war 没有引发错误,我尝试请求 http://hostname:8080/rest/categoryservice/category/01 .这会导致 404 响应。我附上了我的 web.xml、beans.xml 和服务类的片段。 pom 仅包含依赖项。我希望你有任何建议或提示来找出错误。

服务等级:

@Path("/categoryservice")
@Produces({"application/json","application/xml"})
public class CategoryService {
@GET
@Path("/category/{id}")
@Produces({"application/json","application/xml"})
public Category getCategory(@PathParam("id") String id) {
...

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>

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

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</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: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">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


<jaxrs:server id="categoryRESTService" address="/">
<jaxrs:serviceBeans>
<ref bean="categoryService" />
</jaxrs:serviceBeans>
</jaxrs:server>


<bean id="categoryService" class="demo.restful.CategoryService">
<property name="categoryDAO">
<ref bean="categoryDAO" />
</property>
</bean>

最佳答案

此 URL 应该会命中您的服务: http://hostname:8080/rest/categoryservice/category/1

这个 URL 应该返回一个 _wadl http://hostname:8080/rest/categoryservice?_wadl

此 URL 应返回可用服务列表 http://hostname:8080/rest/categoryservice/

通常我会为我的 url-pattern 示例提供更具体的内容:

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/restapi/*</url-pattern>
</servlet-mapping>

在这种情况下,此 URL 应返回可用服务列表 http://hostname:8080/rest/restapi/categoryservice/

关于spring - 使用 Maven 和 tomcat 的 CXF Rest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29949961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com