从ws.url获取空指针异常

问题描述 投票:0回答:1
@Inject
private WSClient ws;

@Override
public void getTerminalDetails(String terminalId, String institutionId) {

    String url = "http://localhost:8089/getDetails";
    WSRequest request = ws.url(url);
    CompletionStage<WSResponse>  wsResponse = request.get();
}

我得到以下错误。

Caused by: java.lang.NullPointerException: null at service.impl.TerminalDetailsServiceImpl.getTerminalDetails(TerminalDetailsServiceImpl.java:25)

第25行是。

WSRequest request = ws.url(url);

谁能帮帮我,我到底错在哪里?我是从SpringBoot迁移过来的Play框架新手。

java spring-boot playframework get
1个回答
0
投票

问题是我没有声明一个注入构造,正如VM4所指出的那样。

代码块应该是这样的。

public class DetailsServiceImpl implements DetailsService,WSBodyReadables {

private final WSClient ws;
private final Config config;

@Inject
public DetailsServiceImpl(WSClient ws, Config config) {
    this.ws = ws;
    this.config = config;
}


@Override
public GetDetailsResonse getDetails(String param1) {
    WSRequest request = ws.url("URL here").setRequestTimeout(Duration.of(5000, ChronoUnit.MILLIS));
    CompletionStage<JsonNode> response = request.get().thenApply(wsResponse -> wsResponse.getBody(json()));
    log.info(response.toString());
    GetDetailsResonse getDetailsResonse = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).convertValue(response.toCompletableFuture().join(), GetDetailsResonse.class);
    return getDetailsResonse ;
}
© www.soinside.com 2019 - 2024. All rights reserved.