0
votes

after creating my rest controller i test it in post man and i get this warnning in my eclipse console org.springframework.web.servlet.DispatcherServlet noHandlerFound "No mapping found for HTTP request with URI [/Project/MyController/1] in DispatcherServlet with name 'springmvc'"

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

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

1
You are hitting a URL that doesn't have any mapping in your controller. Please share the controller code.Minar Mahmud
@MinarMahmud i add the controller in the quetion.taha
I think you are attempting to call searchById. In that case hit this URL /books/1Minar Mahmud
i solve the problem .i had scan the wrong controller package.taha
Great! Please add an answer and mark that as accepted! Happy coding!Minar Mahmud

1 Answers

0
votes

this is the project architecture

enter image description here

i solve the problem .i had scan the wrong controller package.
old 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>

the new file

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