2016年5月

使用web.xml方式配置Spring Data Rest的方式

直接将一下代码加入到web.xml中,可以实现Spring Data Rest拦截/rest地址作为数据访问接口。

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

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

Spring Data Rest暴露主键配置方法

下面的代码通过exposeIdsFor函数暴露了实体类的主键。

@Configuration
class RestMvcConfiguration{

    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {

        return new RepositoryRestConfigurerAdapter() {
            @Override
            public void configureRepositoryRestConfiguration(
                    RepositoryRestConfiguration config) {
                config
                        .exposeIdsFor(CompositeCase.class)
                        .exposeIdsFor(ExperimentAnswer.class)
                        .exposeIdsFor(Classes.class)
                        .exposeIdsFor(Exercise.class)
                        .exposeIdsFor(ExerciseAnswer.class)
                        .exposeIdsFor(Homework.class)
                        .exposeIdsFor(HomeworkAnswer.class)
                        .exposeIdsFor(Knowledge.class)
                        .exposeIdsFor(Problem.class)
                        .exposeIdsFor(Experiment.class)
                        .exposeIdsFor(Subject.class);

            }
        };
    }
}