简体   繁体   English

将REST与spring-mvc绑定返回404源服务器未找到当前服务器

[英]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; 我正在尝试使用Spring MVC和Maven Web项目构建Rest API; however, when I try to run it on server it returns 404 on any url. 但是,当我尝试在服务器上运行它时,它将在任何URL上返回404。

"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. 该项目是使用maven Web项目在Eclipse上构建的,使用spring-mvc并使用数据库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 以下是我放入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>

Below is my applicationContext.xml that I put inside src/main/resources/com/test/restlearning/applicationContext 下面是我的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>

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. 看起来在应用程序上下文中没有bean PersonService Add PersonService class to applicationContext.xml as bean. PersonService类作为bean添加到applicationContext.xml中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Tomcat 404 源服务器没有找到当前的表示 - Spring Tomcat 404 the origin server did not find a current representation 错误404原始服务器未找到当前表示 - Error 404 the origin server did not find a current representation 错误404 Spring-MVC - Error 404 Spring-MVC 如何修复 404 错误,源服务器未找到目标资源的当前表示或不愿意透露存在该表示 - How to fix 404 error, The origin server did not find a current representation for the target resource or is not willing to disclose that one exists HTTP状态404 –找不到:原始服务器未找到目标资源的当前表示形式 - HTTP Status 404 – Not Found : The origin server did not find a current representation for the target resource 如何对HTTP 404进行故障排除原始服务器未找到目标资源的当前表示形式 - How to troubleshoot HTTP 404 The origin server did not find a current representation for the target resource 得到错误 404 源服务器没有找到目标资源的当前表示或不愿意透露存在的表示 - getting error 404 The origin server did not find a current representation for the target resource or is not willing to disclose that one exists 错误 404:源服务器没有找到目标资源的当前表示或不愿意透露存在的表示 - Error 404: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists docker 404 错误。 Tomcat - 源服务器没有找到目标的当前表示 - docker 404 error. Tomcat - The origin server did not find a current representation for the target tomcat错误404-原始服务器找不到目标资源的当前表示,或者不愿意透露存在 - tomcat error 404 - The origin server did not find a current representation for the target resource or is not willing to disclose that one exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM