简体   繁体   中英

Buling REST with spring-mvc returns 404 The origin server did not find a current

I am trying to build rest api with spring mvc and maven web project; however, when I try to run it on server it returns 404 on any url.

"The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."

I've tried to find the solution on the internet and none of them worked for me. I think there might be some mistakes in my configuration but I don't know what. The project is build on eclipse using maven web project, using spring-mvc and hibernate with database mysql. I am kind of confused because there is no error in the log.

Below is my web.xml that I put inside src/main/webapp/WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<servlet>
    <servlet-name>Dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/com/test/restlearning/applicationContext/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>



</web-app>

Below is my applicationContext.xml that I put inside src/main/resources/com/test/restlearning/applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-5.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-5.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-5.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package="com.test.restlearning.controller" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean> 
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/restlearning" />
    <property name="username" value="root" />
    <property name="password" value="" /> 
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.test.restlearning.dto"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="personDao" class="com.test.restlearning.dao.impl.PersonDAOImpl">
    <constructor-arg ref="sessionFactory" />
</bean>


<bean id="personService" class="com.test.restlearning.service.impl.PersonServiceImpl">
    <consrtuctor-arg ref="personDao" />
</bean>

and below is one of my controller

@CrossOrigin
@RestController
@RequestMapping("/persons") 
public class PersonController {
@Autowired
@Qualifier("personService")
private PersonService personService;

@RequestMapping(method= RequestMethod.GET, headers = "Accept=application/json")
public Object getAll(HttpServletRequest request, HttpServletResponse response) {
    try {
        List<Person> persons = personService.getAll();
        String url = ServletUtil.unwrap(request);
        String json = PersonJSONWrapper.wrap(persons,url);
        return ResponseEntity.ok(json);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
    }
}

public static String getURL() {
    return "/persons";
}

public static String getURL(Integer id) {
    return "/persons"+id.toString();
}

}

I would be glad if you could point out my mistakes.

It looks like there is no bean PersonService in application context. Add PersonService class to applicationContext.xml as bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM