2
我有異步端點的Spring MVC應用程序:如何將http會話傳遞給DeferredResult?
@GetMapping
public DeferredResult<Collection<B>> get() {
DeferredResult<Collection<B>> result = new DeferredResult<>();
Executors.newSingleThreadExecutor().submit(() -> result.setResult(service.getB()));
return result;
}
我想偷懶序列化對象與jackson-datatype-hibernate:
@Entity
@Table
public class B {
@Id
@GeneratedValue
private UUID id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id")
private A a;
public A getA() {
return a;
}
}
但我發現了:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.example.demo.B["a"])
錯誤與'Hibernate Session'相關,而不是'HTTP Session';基本上你也試圖在''a''屬性'B'類中''a'屬性,因爲你有一個'Lazy'獲取策略,你會得到錯誤 –
@AngeloImmediata實際上它可以與Callable或CompletableFuture一起工作,當我不試圖執行新線程中的任務。我使用com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module添加了Bean MappingJackson2HttpMessageConverter與自定義ObjectMapper。 –
這很奇怪..錯誤非常明顯,並且抓取策略總是很清晰。你是否檢查過它的工作原理時,所有參數都與不工作的測試相同? –