![](/img/trans.png)
[英]Spring Tomcat 404 the origin server did not find a current representation
[英]Buling REST with spring-mvc returns 404 The origin server did not find a current
我正在尝试使用Spring MVC和Maven Web项目构建Rest API; 但是,当我尝试在服务器上运行它时,它将在任何URL上返回404。
“原始服务器找不到目标资源的当前表示,或不愿意透露该资源的存在。”
我试图在互联网上找到解决方案,但没有一个对我有用。 我认为我的配置中可能存在一些错误,但我不知道是什么。 该项目是使用maven Web项目在Eclipse上构建的,使用spring-mvc并使用数据库mysql进行休眠。 我有点困惑,因为日志中没有错误。
以下是我放入src / main / webapp / WEB-INF的 web.xml
<?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>
下面是我的applicationContext.xml,我把里面的src /主/资源/ COM /测试/ 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>
下面是我的控制器之一
@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();
}
}
如果您能指出我的错误,我将很高兴。
看起来在应用程序上下文中没有bean PersonService 。 将PersonService类作为bean添加到applicationContext.xml中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.