即使日志有 @ManyToOne 懒惰关联资源的结果,也黑屏 [英] Blank Screen even though log has results for @ManyToOne Lazy associated resource
问题描述
我正在使用以下依赖项 -
I am using following dependencies -
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.0.M1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.5.0-rc1</version>
</dependency>
并发现一些多对一链接(那些对应的存储库没有摘录投影的链接)即使在 Hibernate Trace 中显示正在提取结果,但不会在屏幕上呈现 JSON 结果并且只显示空白页面,直到我选择其中的一个投影查询参数.
And Find that some ManyToOne Links (those whose corresponding Repositories dont have Excerpt Projections) even though show in Hibernate Trace are extracting results but don't render JSON results on screen and presents only blank page until I choose one of its projections in query param.
no output but query are shown in log returning result with Fetch Type as LAZY
http://localhost:8585/app/userLanguages/8/user
get JSON as expected on screen with user detail as well
http://localhost:8585/app/userLanguages/8/user?projection=summary
get output with Fetch Type as EAGER
http://localhost:8585/app/userLanguages/8/user
我还为自定义 REST url 打印格式化的 JSON 输出启用了漂亮的打印,希望这不会导致任何问题.
I have also enabled pretty print for printing formatted JSON output for custom REST urls, hoping this should not cause any issue.
<mvc:annotation-driven>
<mvc:message-converters>
<bean id="jacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prettyPrint" value="true" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
实体也有以下注释 -
@AccessType(Type.PROPERTY)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)
上面使用的投影不是摘录投影
Projections used above were not excerpt projections
推荐答案
经过一些调试发现这是由于 UserResourceProcessor 导致的.我曾尝试通过下面添加一些额外的链接来指向自定义 REST URL.一旦我评论了@Component 注释,并且在实体中只保留了 @AccessType(Type.PROPERTY) 注释,即使是延迟加载 URL 也会按预期工作.但是,我需要自定义链接,这是一个问题..
After some debugging found out that this is happening due to UserResourceProcessor. I had tried adding some extra links for pointing to custom REST URL through below. Once I commented @Component annotation, and kept only @AccessType(Type.PROPERTY) annotation in Entity, even lazy load URL work as intended. But, I will need the custom links and this is an issue ..
@Component
public class UserResourceProcessor implements ResourceProcessor<Resource<User>> {
public static final String CANCEL_REL = "cancel";
public static final String UPDATE_REL = "update";
@Autowired
private EntityLinks entityLinks;
@Override
public Resource<User> process(Resource<User> resource) {
User user = resource.getContent();
resource.add(entityLinks.linkForSingleResource(user).withRel(CANCEL_REL));
resource.add(entityLinks.linkForSingleResource(user).withRel(UPDATE_REL));
return resource;
}
}
这篇关于即使日志有 @ManyToOne 懒惰关联资源的结果,也黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!