0

创建我的休息控制器后,我在邮递员中对其进行测试,并在我的 Eclipse 控制台 org.springframework.web.servlet.DispatcherServlet noHandlerFound 中收到此警告“在 DispatcherServlet 中找不到带有 URI [/Project/MyController/1] 的 HTTP 请求的映射名称'springmvc'"

我的 springmvc-servlet.XML

    <?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="books" />
    </bean>
    <tx:annotation-driven />
    <context:component-scan
        base-package="src/main/java/dao" />
    <context:component-scan
        base-package="src/main/java/service" />
    <context:component-scan
        base-package="src/main/java/controllers" />
    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
            ref="entityManagerFactory" />
    </bean>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="jsonMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
    <bean id="xmlMessageConverter"
        class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter" />
                <ref bean="xmlMessageConverter" />
            </list>
        </property>
    </bean>
</beans>

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:web="http://java.sun.com/xml/ns/javaee"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <display-name>Archetype Created Web Application</display-name>
    <servlet>​
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
my rest controller

    package controllers;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import model.Book;
    import service.IBookService;

    @RestController
    @RequestMapping("/books")
    public class BookRestController {
        @Autowired
        IBookService service;

        @PostMapping(path="/add")
        public void add(@RequestBody Book b) {
            service.add(b);
        }

        @GetMapping(path="/{id}")
        public Book searchById(@PathVariable("id") int id) {
            return service.recherche(id);
        }
    }

i use spring framework. 

4

1 回答 1

0

这是项目架构

在此处输入图像描述

我解决了问题。我扫描了错误的控制器包。
旧 springmvc-servlet.xml

    </bean>
<tx:annotation-driven />
<context:component-scan
    base-package="src/main/java/dao" />
<context:component-scan
    base-package="src/main/java/service" />
<context:component-scan
    base-package="src/main/java/controllers" />
<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>

新文件

    </bean>
<tx:annotation-driven />
<context:component-scan
    base-package="dao" />
<context:component-scan
    base-package="service" />
<context:component-scan
    base-package="controllers" />
<bean id="transactionManager"
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory"
        ref="entityManagerFactory" />
</bean>
于 2020-06-03T17:24:41.790 回答