任何幫助請!
當我呼叫在后臺呼叫 Feign 的端點時,我收到此錯誤:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
`org.springframework.http.ResponseEntity` (no Creators, like default constructor, exist): cannot deserialize
from Object value (no delegate- or property-based Creator)
at [Source: (BufferedReader); line: 1, column: 2]
這是我在 Controller 中的端點:
@RestController
@RequestMapping(Routes.URI_PREFIX)
public class CartoController {
@Autowired
private ReadCartographyApiDelegate readCartographyApiDelegate;
@GetMapping(value = "/cartographies/{uid}", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseWrapper<ReadCartographyResponse> readCarto(HttpServletRequest request,
@PathVariable(name = "uid") String uid) {
ResponseEntity<ReadCartographyResponse> result ;
try {
result = readCartographyApiDelegate.readCartography(uid);
}catch (Exception e){
throw new TechnicalException("Error during read Carto");
}
return responseWrapperWithIdBuilder.of(result.getBody());
}
}
介面 ReadCartographyApiDelegate 由 openApi 從 yaml 檔案自動生成:
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "...")
public interface ReadCartographyApiDelegate {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
default ResponseEntity<ReadCartographyResponse> readCartography(String uid) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
String exampleString = "null";
ApiUtil.setExampleResponse(request, "application/json", exampleString);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
這是我的 ReadCartoApiDelegateImpl ,它實作了 ReadCartographyApiDelegate 介面:
@Service
public class ReadCartographyApiDelegateImpl implements ReadCartographyApiDelegate {
private EcomGtmClient ecomGtmClient;
public ReadCartographyApiDelegateImpl(EcomGtmClient ecomGtmClient) {
this.ecomGtmClient = ecomGtmClient;
}
@Override
public ResponseEntity<ReadCartographyResponse> readCartography(String uid) {
ResponseEntity<ReadCartographyResponse> response = ecomGtmClient.readCartography(uid);
return response;
}
}
這是假客戶:
@FeignClient(name = "ecomGtmSvc", url = "http://localhost/")
public interface EcomGtmClient {
@GetMapping(value = "/read-carto/{uid}")
ResponseEntity<ReadCartographyResponse> readCartography(@PathVariable("uid") String uid);
}
問題是 ResponseEntity (彈簧類)類不包含創建實體時需要的默認建構式。是否有任何配置可以解決此問題?
uj5u.com熱心網友回復:
如果您想訪問 feign 回應的正文或標題,您應該使用feign.Response該類。ResponseEntity不適用于 feign,因為它不是故意的。我認為最好Response從你的假客戶端方法回傳。然后,您應該能夠將主體傳遞給控制器??中的 ResponseEntity 實體。
您甚至使用回應包裝器的原因是什么,我無法從您的代碼中弄清楚這一點?
遺憾的是,我在 Response 類上找不到任何檔案,但這里是 GitHub 上源代碼的鏈接。 https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/Response.java
我的建議是
@FeignClient(name = "ecomGtmSvc", url = "http://localhost/")
public interface EcomGtmClient {
@GetMapping(value = "/read-carto/{uid}")
ReadCartographyResponse readCartography(@PathVariable("uid") String uid);
}
@RestController
@RequestMapping(Routes.URI_PREFIX)
public class CartoController {
@Autowired
private ReadCartographyApiDelegate readCartographyApiDelegate;
@GetMapping(value = "/cartographies/{uid}", produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseWrapper<ReadCartographyResponse> readCarto(HttpServletRequest request,
@PathVariable(name = "uid") String uid) {
ReadCartographyResponse result ;
try {
result = readCartographyApiDelegate.readCartography(uid);
}catch (Exception e){
throw new TechnicalException("Error during read Carto");
}
// I don't know where you get the builder from, so I assume it does something import and is needed
return responseWrapperWithIdBuilder.of(result);
}
}
當然,您還必須更改所有中間類。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522036.html
上一篇:找不到模板位置:(請添加一些模板,檢查您的Thymeleaf配置,或設定spring.thymeleaf.check-template-location=false)
