0
votes

Why does the handler method not get found?

I have 404 error while using spring restful, I have already tried most answers, but for some reason, nothing works for me

Here are my configuration:

My web.xml configuration

<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/config/dispatcherServlet.xml
        </param-value>
    </init-param>
    <init-param>
        <param-name>javaEncoding</param-name>
        <param-value>UTF-8</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>

And

My dispatcher-servlet.xml configuration

<mvc:resources mapping="/img/**" location="/img/*" />
<mvc:resources mapping="/js/**" location="/js/*" />
<mvc:resources mapping="/ajax/**" location="/ajax/*" />

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.crscube.etmf.*"
    use-default-filters="false">
</context:component-scan>

<!-- <mvc:annotation-driven /> -->

<!-- Spring MVC @Controller 매핑 -->
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

And My Controller source

@Controller
public class UserController {

protected final Logger logger = Logger.getLogger(getClass());

@Autowired
UserService userService = new UserServiceImpl();

@ResponseBody
@RequestMapping(value="/users", method=RequestMethod.GET)
public List<UserModel> getUser() throws Exception{

    List<UserModel> listOfUser = userService.getUserList();

    return listOfUser;

}

The problem is that requesting http://localhost:8080/etmf/users gives me just:

DEBUG ({http-bio-8080-exec-2} DispatcherServlet.java[doService]:819) [2017-02-07 19:36:08,466] - DispatcherServlet with name 'dispatcher' processing GET request for [/etmf/users] DEBUG ({http-bio-8080-exec-2} AbstractHandlerMethodMapping.java[getHandlerInternal]:213) [2017-02-07 19:36:08,487] - Looking up handler method for path /users DEBUG ({http-bio-8080-exec-2} AbstractHandlerMethodMapping.java[getHandlerInternal]:223) [2017-02-07 19:36:08,509] - Did not find handler method for [/users] DEBUG ({http-bio-8080-exec-2} AbstractUrlHandlerMapping.java[lookupHandler]:169) [2017-02-07 19:36:08,541] - Matching patterns for request [/users] are [/**] DEBUG ({http-bio-8080-exec-2} AbstractUrlHandlerMapping.java[lookupHandler]:193) [2017-02-07 19:36:08,564] - URI Template variables for request [/users] are {} DEBUG ({http-bio-8080-exec-2} AbstractUrlHandlerMapping.java[getHandlerInternal]:124) [2017-02-07 19:36:08,585] - Mapping [/users] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@15a63e4a] and 1 interceptor DEBUG ({http-bio-8080-exec-2} DispatcherServlet.java[doDispatch]:902) [2017-02-07 19:36:08,609] - Last-Modified value for [/etmf/users] is: -1 DEBUG ({http-bio-8080-exec-2} DispatcherServlet.java[doDispatch]:957) [2017-02-07 19:36:08,631] - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling DEBUG ({http-bio-8080-exec-2} FrameworkServlet.java[processRequest]:913) [2017-02-07 19:36:08,650] - Successfully completed request

How can I solve this problem?

Thanks.

1
Your @RequestMapping value is "/users", but you try to open /etmf/users - Optio
Let me know if below answer worked for you.. - Vijendra Kumar Kulhade

1 Answers

0
votes

I think you need 2 corrections in your dispatcher-servlet.xml configuration. 1. base-package="com.crscube.etmf" 2. MappingJackson2HttpMessageConverter Copy paste below code in dispatcher-servlet.xml file and try again.

<mvc:resources mapping="/ajax/**" location="/ajax/*" />

<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.crscube.etmf"
    use-default-filters="false">
</context:component-scan>

<!-- <mvc:annotation-driven /> -->

<!-- Spring MVC @Controller 매핑 -->
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler />