Spring-Rest应用程序中的404错误

时间:2014-11-20 11:45:34

标签: java spring rest

我有一个简单的应用程序显示rest + spring.but得到404错误: 在运行时它应该给出jeresey + spring作为输出 web xml文件

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.spring.container.servlet.SpringServlet
        </servlet-class>
        <init-param>
            <param-name>
                                 com.sun.jersey.config.property.packages
                        </param-name>
            <param-value>com.mtv.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

应用程序上下文文件是

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.mtv.rest" />

        <bean id="transactionBo" 
                  class="com.mtv.rest.TransactionBoImpl" />

</beans>

付款类

package com.mtv.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mtv.rest.TransactionBo;

@Component
@Path("/payment")
public class PaymentService {

    @Autowired
    TransactionBo transactionBo;

    @GET
    @Path("/mtv")
    public Response savePayment() {

        String result = transactionBo.save();

        return Response.status(200).entity(result).build();

    }

}

界面

package com.mtv.rest;

public class TransactionBoImpl implements TransactionBo {

    public String save() {

        return "Jersey + Spring example";

    }


}

有时tomcat端口也会出错。

2 个答案:

答案 0 :(得分:1)

  • 正确检查是否已正确添加所有 JAR文件/依赖项
  • 检查它applicationContext.xml是否在适当的位置,即在src文件夹中。

这是一些最常见的错误。

答案 1 :(得分:0)

尝试这种方式,您需要定义一个控制器:

@Controller
@RequestMapping(value = "/payment")
public class PaymentService { // should be called PaymentController?

    @Autowired
    TransactionBo transactionBo;

    @RequestMapping(method = RequestMethod.GET, value = "/mtv")
    @ResponseBody
    public Response savePayment(HttpServletRequest request, HttpServletResponse response) {

        /// your code...

    }

}

看看这个spring-mvc-showcase