@QueryResult映射出错

时间:2016-06-03 08:35:17

标签: java spring neo4j spring-boot spring-data-neo4j-4

我正在使用

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-server</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

当我使用查询进行单元测试时,我得到的例外是

10:27:24.803 [http-nio-8080-exec-8] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/rest-api] threw exception [Request processing failed; nested exception is org.neo4j.ogm.metadata.MappingException: Error mapping to ad-hoc class com.lyreco.lab.neo4j.entity.CategoryResult.  At present, only @QueryResult types that are discovered by the domain entity package scanning can be mapped.] with root cause
org.neo4j.ogm.metadata.MappingException: Error mapping to ad-hoc class com.lyreco.lab.neo4j.entity.CategoryResult.  At present, only @QueryResult types that are discovered by the domain entity package scanning can be mapped.

这是我的CategoryResult类

package com.lyreco.lab.neo4j.entity;
import org.springframework.data.neo4j.annotation.QueryResult;
import com.lyreco.lab.bean.CategoryVO;

@QueryResult
public class CategoryResult {
    private Long neo4jId;
    private String uuid;
    private String name;
    private CategoryVO parent = new CategoryVO();
    private long numberItems;
...

这是我的Application类

package com.lyreco.lab.configuration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableHypermediaSupport(type = { EnableHypermediaSupport.HypermediaType.HAL })
@EnableSpringDataWebSupport
@ComponentScan(basePackages = { "com.lyreco.lab.api", "com.lyreco.lab.service", "com.lyreco.lab.neo4j",
        "com.lyreco.lab.security" })
@Import({ SwaggerConfig.class, Neo4jConfig.class, SpringSecurityConfig.class })
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {

        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

                container.addErrorPages(error401Page, error404Page, error500Page);
            }
        };
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {

            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/rest-api/**").allowedOrigins("http://localhost:9000");
            }
        };
    }
}

这是我的SessionFactory

package com.lyreco.lab.configuration;

import java.util.UUID;

import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.event.BeforeSaveEvent;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.data.neo4j.server.RemoteServer;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.data.neo4j.template.Neo4jTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.lyreco.lab.neo4j.entity.DomainEntity;

@Configuration
@EnableNeo4jRepositories("com.lyreco.lab.neo4j.repository")
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
class Neo4jConfig extends Neo4jConfiguration {

    @Value("${neo4j.server.url}")
    private String serverAddress;

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("com.lyreco.lab.neo4j.entity");
    }

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer(serverAddress);
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    ApplicationListener<BeforeSaveEvent> beforeSaveEventApplicationListener() {
        return new ApplicationListener<BeforeSaveEvent>() {
            @Override
            public void onApplicationEvent(BeforeSaveEvent event) {
                DomainEntity entity = (DomainEntity) event.getEntity();
                entity.setUuid(UUID.randomUUID().toString());
            }
        };
    }

    // TODO : Bug fix
    // cf.
    // http://stackoverflow.com/questions/30604863/spring-data-neo4j-4-0-0-beforesaveevent-not-firing
    // cf. https://jira.spring.io/browse/DATAGRAPH-710
    @Bean
    public Neo4jOperations getNeo4jTemplate() throws Exception {
        return new Neo4jTemplate(getSession());
    }
}

不确定我的代码有什么问题。

1 个答案:

答案 0 :(得分:3)

此异常表示不扫描包含@QueryResult com.lyreco.lab.neo4j.entity的包。请查看提供给SessionFactory的软件包列表。