404 error in Spring-Rest Application

2019-08-09 18:59发布

问题:

I have a simple application of displaying rest+spring.but getting 404 error: on run it should give jeresey+spring as output web xml file

<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>

application context file is

<?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>

payment class

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();

    }

}

interface is

package com.mtv.rest;

public class TransactionBoImpl implements TransactionBo {

    public String save() {

        return "Jersey + Spring example";

    }


}

also sometimes tomcat port gets error.

回答1:

  • Check properly if you have added all the JAR files/dependencies properly
  • Check it applicationContext.xml is at proper place i.e in src folder.

This are some of the most common mistakes.



回答2:

try this way, you need to define a controller:

@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...

    }

}

Look at this spring-mvc-showcase



标签: java spring rest