0

我在我的 spring-mvc 应用程序中使用 spring-data-rest(1.0.0.RELEASE) 但我遇到了以下问题

在我的 WebConfig

@Import(RepositoryRestMvcConfiguration.class)
public static class WebConfiguration extends WebMvcConfigurationSupport{...}

这里这个案例RepositoryRestMvcConfiguration有一个bean RepositoryRestController,它的方法listRepositories(...)用注释

@RequestMapping(value = "/", method = RequestMethod.GET)

现在的问题是,当我点击根上下文("/")时,我得到了指向我的存储库的链接,如下所示

{
  "links" : [ {
    "rel" : "content",
    "href" : "http://localhost:7070/appName/content"
  }, {
    "rel" : "language",
    "href" : "http://localhost:7070/appName/language"
  } ],
  "content" : [ ]

}

但我想显示 index.html 文件。

我想禁用在根上下文中发现链接。

我尝试使用自定义控制器映射到根上下文的事件,("/")但第一优先级的 spring 与该RepositoryRestController's.listRepositories(...)方法匹配。

它没有进入我的控制器方法。在日志中它是这样的

**RepositoryRestHandlerMapping**: 185 - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryRestController.listRepositories(org.springframework.http.server.ServletServerHttpRequest,java.net.URI) throws java.io.IOException
**RequestMappingHandlerMapping**: 185 - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String xxx.xx.xx.AccessController.getIndex()
4

2 回答 2

0

我遇到了同样的问题,并解决了如下:

1) 切换到 spring-data-rest-webmvc 1.1.0.M1

2) 将您的上下文配置拆分为 web-config 和 rest-config

WebConfig.java

@Configuration
@EnableHypermediaSupport
@EnableSpringDataWebSupport
@EnableWebMvc
@ComponentScan(basePackages = {"com.yourcompanyname.XXX"})
public class WebConfig extends WebMvcConfigurationSupport {

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass( JstlView.class );
    viewResolver.setPrefix( "/WEB-INF/views/" );
    viewResolver.setSuffix( ".jsp" );
    return viewResolver;
}

@Bean
public ReloadableResourceBundleMessageSource messageSource()
{
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename( "/resources/messages" );
    messageSource.setCacheSeconds( 0 );
    return messageSource;
}

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON);
}


@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {

    RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping() {

        @Override
        protected boolean isHandler(Class<?> beanType) {
            return beanType == null || ClassUtils.getPackageName(beanType).startsWith("org.springframework.data") ? false
                    : super.isHandler(beanType);
        }
    };

    handlerMapping.setOrder(0);
    handlerMapping.setInterceptors(getInterceptors());
    handlerMapping.setContentNegotiationManager(mvcContentNegotiationManager());

    return handlerMapping;
}

@Override
@Bean
public HandlerMapping resourceHandlerMapping() {
    AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) super.resourceHandlerMapping();
    handlerMapping.setOrder(-1);
    return handlerMapping;
}

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}

RESTConfig.java

@Configuration
public class RESTConfig extends RepositoryRestMvcConfiguration {
}

然后创建 webapp 初始化器类来替换 web.xml 中的注册(需要 servlet-api 3.0+)。在以下示例中,spring-data-rest 路径设置为 /rest/ ,还包括我自己建议的 JPA 配置类。我们将在 2 个单独的类中注册 2 个 servlet,一个用于主“/”,一个用于“/rest/ ”路径:

WebAppInitializer.java

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getRootConfigClasses()
*/
@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { JpaConfig.class };
}

/*
 * (non-Javadoc)
 * @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getServletConfigClasses()
 */
@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
}

/*
 * (non-Javadoc)
 * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletMappings()
 */
@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}



/*
 * (non-Javadoc)
 * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletFilters()
 */
@Override
protected javax.servlet.Filter[] getServletFilters() {
    return new javax.servlet.Filter[] { new OpenEntityManagerInViewFilter() };
}

}

RestAppConfig.java

public class RestAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

/*
* (non-Javadoc)
* @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getRootConfigClasses()
*/
@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { JpaConfig.class };
}

/*
 * (non-Javadoc)
 * @see org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer#getServletConfigClasses()
 */
@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { RESTConfig.class };
}

/*
 * (non-Javadoc)
 * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletMappings()
 */
@Override
protected String[] getServletMappings() {
    return new String[] { "/rest/*" };
}



/*
 * (non-Javadoc)
 * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer#getServletFilters()
 */
@Override
protected javax.servlet.Filter[] getServletFilters() {
    return new javax.servlet.Filter[] { new OpenEntityManagerInViewFilter() };
}

}

于 2013-07-02T13:48:03.083 回答
0

这是我实现这一目标的方法

  1. 子类 RepositoryRestMvcConfiguration,然后抛出一些自定义异常

  2. 在 Advised Controller 中捕获异常并返回“index.html”

  3. 使用子类在配置中导入

第1步

@Configuration
public class CustomRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {
    @Override
    public RepositoryRestController repositoryRestController() throws Exception {
        RepositoryRestController restController = new RepositoryRestController(){
            @Override
            public ResponseEntity<?> listRepositories(ServletServerHttpRequest request, URI baseUri) throws IOException {
                throw new RootContextRepositoryException();                
            }
        };

        return restController;
    }
}

第2步

@ControllerAdvice
public class BaseAdvisedController {

    @ExceptionHandler({RootContextRepositoryException.class})
    public String rootContextRepositoryExceptionHandler(RootContextRepositoryException ex){
        return "index.html";
    }

}

第 3 步

@Import(CustomRepositoryRestMvcConfiguration.class)
public static class WebConfiguration extends WebMvcConfigurationSupport{...}

这解决了我的问题

于 2013-07-04T05:07:55.190 回答