如果将在SpringWebFlux中使用的Entity类的成员设为final,会出现错误
这已经完全是我自己的备忘录了。虽然可以在Twitter上留下,但为了保留,这里留一份。
如果随意创建Spring Data MongoDB中使用的实体类,会出现一些错误。
会出现一些要求提供Setter方法的错误。
试着加上Setter方法确实能成功运行。
但只是执行findOne然后返回结果,为什么会出现这种情况???
This application has no configured error view, so you are seeing this as a fallback.
Mon Feb 18 06:27:46 JST 2019
There was an unexpected error (type=Internal Server Error, status=500).
No accessor to set property @org.springframework.data.annotation.Id()private final java.lang.Integer com.example.demo.Flower.id!
java.lang.UnsupportedOperationException: No accessor to set property @org.springframework.data.annotation.Id()private final java.lang.Integer com.example.demo.Flower.id!
at com.example.demo.Flower_Accessor_jjo0e1.setProperty(Unknown Source)
at org.springframework.data.mapping.model.ConvertingPropertyAccessor.setProperty(ConvertingPropertyAccessor.java:61)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readAndPopulateIdentifier(MappingMongoConverter.java:326)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:289)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:275)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:245)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:194)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
at org.springframework.data.mongodb.core.ReactiveMongoTemplate$ReadDocumentCallback.doWith(ReactiveMongoTemplate.java:2910)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:107)
at reactor.core.publisher.FluxOnAssembly$OnAssemblySubscriber.onNext(FluxOnAssembly.java:353)
at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76)
at com.mongodb.reactivestreams.client.internal.ObservableToPublisher$1.onNext(ObservableToPublisher.java:68)
...
稍微改动一下代码,再仔细读读错误信息…
啊,原来如果把它设为final,在转换这一部分会有问题。我大概了解了。
没有setter,只要把final去掉就可以工作了。背后有很多事情呢。
@Document
public class Flower {
@Id
private Integer id;
private String name;
private String color;
Flower(){
id = null;
name = "";
color = "";
}
Flower(int id, String name, String color){
this.id = id;
this.name = name;
this.color = color;
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public String getColor() {
return this.color;
}
}