当前位置: 首页 > 知识库问答 >
问题:

使用简单的spring-data-rest项目获取404错误

景鸿才
2023-03-14

我试图在我的spring-mvc项目上配置spring-data-rest,但是每次我输入访问我的repository-info-it的json的url时都返回404。看起来它根本看不到我的spring-data-rest注释/依赖项

这是我的pom.xml

<groupId>com.konan</groupId>
<artifactId>libsupport</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>LibSupport</name>
<description>Library Support System</description>

<properties>
    <org.springframework.version>4.2.0.RELEASE</org.springframework.version>
</properties>

<dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.8.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>2.4.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
        <version>0.17.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.10.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.10.Final</version>
    </dependency>

    <!-- Logger -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/lib-support</path>
                <contextReloadable>true</contextReloadable>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
@RepositoryRestResource(path = "books", collectionResourceRel = "books")
public interface BookRepository extends JpaRepository<Book, Integer> { 

}
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{JPAConfig.class,};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackageClasses = BookRepository.class)
@PropertySource("classpath:app.properties")
public class JPAConfig {

    @Autowired
    private Environment environment;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName"));
        driverManagerDataSource.setUrl(environment.getProperty("jdbc.url"));
        driverManagerDataSource.setUsername(environment.getProperty("jdbc.user"));
        driverManagerDataSource.setPassword(environment.getProperty("jdbc.pass"));
        return driverManagerDataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setPersistenceUnitName("localContainerEntityManagerFactoryBean");

        localContainerEntityManagerFactoryBean.setDataSource(dataSource());
        localContainerEntityManagerFactoryBean.setPackagesToScan(environment.getProperty("entities.scan"));

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
        localContainerEntityManagerFactoryBean.setJpaPropertyMap(hibernateProperties());

        return localContainerEntityManagerFactoryBean;
    }


    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }


    private Map<String, String> hibernateProperties() {
        Map<String, String> hibernateProperties = new HashMap<>();
        hibernateProperties.put("hibernate.hbm2ddl.auto", environment.getProperty("hb.hbm2ddl.auto"));
        hibernateProperties.put("hibernate.dialect", environment.getProperty("hb.dialect"));
        hibernateProperties.put("hibernate.show_sql", environment.getProperty("hb.show_sql"));
        hibernateProperties.put("hibernate.format_sql", environment.getProperty("hb.format_sql"));
        hibernateProperties.put("hibernate.enable_lazy_load_no_trans", environment.getProperty("hb.enable_lazy_load_no_trans"));
        return hibernateProperties;
    }
}

@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = {BookController.class, BookService.class})
public class WebConfig extends WebMvcConfigurerAdapter {

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

有人能帮忙吗?提前道谢。

共有1个答案

姬泰
2023-03-14

好了,伙计们,我知道问题出在哪里了。因此,我并没有为所有实体类创建reporitories类,因此Spring无法为相关实体生成适当的链接。例如,我有book实体,该实体将OneTomany映射到BookCopy实体类,但BookCopyRepository不存在。

 类似资料:
  • 在此处输入图像描述 WEB内容: 在此处输入图像描述 我不知道这个项目有什么问题,我发誓在我来这里问这个问题之前,我真的试图搜索结果。希望你们帮帮我,谢谢!

  • 问题内容: Spring Data REST仅自动公开域对象。但是大多数情况下,我们必须处理数据传输对象。那么如何以SDR方式做到这一点呢? 问题答案: Entities 实体必须实现Identifiable接口。例如: Projections 创建一个投影接口,存储库查询方法将返回: 这将是DTO的基础。在此示例中,DTO将代表a Category,而 的数量属于它。 Repository me

  • Spring Data REST只自动公开域对象。但大多数情况下,我们必须处理数据传输对象。那么如何用SDR的方式做到这一点呢?

  • 我试图创建一个带有REST API的基本Spring Boot应用程序(JDK1.8)。下面是我的应用程序代码

  • 我正在学习Spring Boot,但我无法执行基本的“Hello World”程序。我已经使用https://start.spring.io/构建了基本项目,并按照以下代码片段更新了代码: Spring靴主类: 控制器类: POM. xml文件: 错误: 我知道这个问题已经被很多人问过了,但我已经仔细阅读了这些答案,并尝试了那些给出的答案,但没有成功。 使用的技术有Java 8、Tomcat 9、

  • 我有一些构建工件,其中包含一些调试信息,我希望在构建摘要中显示这些信息。然而,我看到的唯一获取工件的API是一个zip文件。如果您转到工件屏幕并浏览工件,然后复制下载URL,您将得到一个API,如下所示: https://{帐户}/_ API/资源/容器/{容器}?itemPath = {文件名} 但是,我似乎找不到REST客户端API来在扩展中的ts脚本中使用此功能。 对如何从中获取实际文件有什