RepositoryRestController无法正常工作

时间:2018-04-10 03:56:59

标签: java spring spring-data-rest

我不会说英语请理解。

这可能没有用,但我会告诉你我的地址。 git

RepositoryRestController不工作。
d.style@BasePathAwareController他们中的任何一个都不起作用 如果在类区域中使用映射,则不能使用其他回购 它在一天前使用相同的来源。

发展环境
java 1.8
弹簧4.3.8
spring data mongo 1.10.3
弹簧数据休息2.6.10
spring hateoas 0.23.0
mongo driver 3.0.2

控制器

@RepositoryRestController

的web.xml

@RepositoryRestController
//@BasePathAwareController
//@RequestMapping(value = "")
public class ArticleController {

@Resource(name="articleCustomRepo")
private ArticleCustomRepository cRepo;

@Resource(name="articleService")
private ArticleService 

    @RequestMapping(value="/articles/{idx}/comments",method=RequestMethod.GET,produces="application/hal+json")
    public @ResponseBody HttpEntity<List<CommentResource>> getComment(@PathVariable("idx") int idx) {

        List<CommentResource> list = cRepo.data(idx);

        return new ResponseEntity<>(list,HttpStatus.OK);
    }

    @RequestMapping(value="/articles",method=RequestMethod.POST,produces="application/hal+json")
    public HttpEntity<ArticleResource> postArticle(@RequestBody Map<String,Object> map) {

            ArticleResource article = service.postArticle(map);

            return new ResponseEntity<>(article, HttpStatus.OK);
        }
    }

上下文config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value><!--  -->classpath*:config/spring/context-config.xml</param-value>
        </context-param>


    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
            <servlet-name>rest</servlet-name>
            <servlet-class>org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet</servlet-class>
        <init-param>
            <param-name>throwExceptionIfNoHandlerFound</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/api/v1/*</url-pattern>
    </servlet-mapping>

</web-app>

SpringConfig.java

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="horizon">
    </context:component-scan>

</beans>

帮我弄清楚如何修复它。

1 个答案:

答案 0 :(得分:0)

  • 似乎你在slf4j和log4j之间有一个调用循环尝试从你的pom.xml中删除log4j依赖项并检查你的tomcat日志

  • 创建自定义调度程序servlet

    public class RestDispatcherServlet extends DispatcherServlet {
     public RestDispatcherServlet() {
      configure();
      }
    
    public RestDispatcherServlet(WebApplicationContext 
        webApplicationContext) {
      super(webApplicationContext);
      configure();
    }
    private void configure() {
    setContextClass(AnnotationConfigWebApplicationContext.class);
    setContextConfigLocation(
    RestMvcConfiguration.class.getName());
     }
    

    }

  • 更新您的web.xml调度程序servlet

  • 创建rest mvc app config

      @ComponentScan("horizon.board.controller")
      public class RestMvcConfiguration extends 
        RepositoryRestMvcConfiguration {
       }