Spring restful配置错误

时间:2017-02-07 10:44:24

标签: java spring-mvc configuration restful-url

为什么找不到处理程序方法?

我在使用spring restful时遇到404错误,我已经尝试了大多数答案,但由于某种原因,没有什么对我有用

以下是我的配置:

我的web.xml配置

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

并且

我的dispatcher-servlet.xml配置

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

我的控制器来源

@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;

}

问题是请求http://localhost:8080/etmf/users只给我:

  

DEBUG({http-bio-8080-exec-2} DispatcherServlet.java [doService]:819)[2017-02-07 19:36:08,466] - 名为'dispatcher'的DispatcherServlet处理GET请求[/ etmf /用户]   DEBUG({http-bio-8080-exec-2} AbstractHandlerMethodMapping.java [getHandlerInternal]:213)[2017-02-07 19:36:08,487] - 查找路径/用户的处理程序方法   DEBUG({http-bio-8080-exec-2} AbstractHandlerMethodMapping.java [getHandlerInternal]:223)[2017-02-07 19:36:08,509] - 没有为[/ users]找到处理程序方法   DEBUG({http-bio-8080-exec-2} AbstractUrlHandlerMapping.java [lookupHandler]:169)[2017-02-07 19:36:08,541] - 请求[/ users]的匹配模式是[/ **]   DEBUG({http-bio-8080-exec-2} AbstractUrlHandlerMapping.java [lookupHandler]:193)[2017-02-07 19:36:08,564] - 请求[/ users]的URI模板变量是{}   DEBUG({http-bio-8080-exec-2} AbstractUrlHandlerMapping.java [getHandlerInternal]:124)[2017-02-07 19:36:08,585] - 使用handler [org.springframework.web]将[/ users]映射到HandlerExecutionChain .servlet.resource.DefaultServletHttpRequestHandler @ 15a63e4a]和1个拦截器   DEBUG({http-bio-8080-exec-2} DispatcherServlet.java [doDispatch]:902)[2017-02-07 19:36:08,609] - [/ etmf / users]的Last-Modified值为:-1   DEBUG({http-bio-8080-exec-2} DispatcherServlet.java [doDispatch]:957)[2017-02-07 19:36:08,631] - Null ModelAndView返回DispatcherServlet,名称为'dispatcher':假设HandlerAdapter已完成请求处理   DEBUG({http-bio-8080-exec-2} FrameworkServlet.java [processRequest]:913)[2017-02-07 19:36:08,650] - 已成功完成请求

我该如何解决这个问题?

感谢。

1 个答案:

答案 0 :(得分:0)

我认为您的dispatcher-servlet.xml配置需要2次更正。 1. base-package =&#34; com.crscube.etmf&#34; 2. MappingJackson2HttpMessageConverter 将代码粘贴到dispatcher-servlet.xml文件中的代码下方,然后重试。

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