【问题标题】:Getting null pointer exception from ws.url从 ws.url 获取空指针异常
【发布时间】:2020-05-12 17:41:09
【问题描述】:
@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();
}

我收到以下错误:

原因:java.lang.NullPointerException: null 在 service.impl.TerminalDetailsS​​erviceImpl.getTerminalDetails(TerminalDetailsS​​erviceImpl.java:25)

第 25 行是:

WSRequest request = ws.url(url);

有人可以帮助我解决我的问题。我是从 SpringBoot 迁移过来的 Play 框架的新手。

【问题讨论】:

  • 你有@Inject 构造函数吗?

标签: java spring-boot playframework get


【解决方案1】:

问题是我没有像 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 ;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多