【问题标题】:NullPointedException when using Spring RestTemplate in Java在 Java 中使用 Spring RestTemplate 时出现 NullPointedException
【发布时间】:2018-03-28 14:26:35
【问题描述】:

我正在使用最新的 Springframework,但在尝试从我的服务器获取 int 时遇到问题。所有代码都是用 Java 编写的。

当我通过浏览器与服务器交互时,一切正常。当通过客户端与服务器交互时,我得到一个NullPointerException

请记住,我是一名软件初学者。

服务器代码(我都试过了,在使用浏览器时效果很好):

public class RestController {

    private GameSession gameSession = new GameSession();

    @RequestMapping(value = "registerPlayer")
    public int registerPlayer(@RequestParam("name") String name, @RequestParam("mode") boolean mode) {
        return gameSession.registerPlayer(name, mode);
    }

    @RequestMapping(value = "registerPlayer/{name}/{mode}")
    public int registerPlayer(@PathVariable String name, @PathVariable boolean mode) {
        return gameSession.registerPlayer(name, mode);
    }

}

客户端代码(再次尝试两者,结果相同):

@Component
public class GameSessionClient implements ISeaBattleGame{

    @Autowired
    private RestOperations restOperations;
    private String url;

    @Override
    public int registerPlayer(String name, boolean singlePlayerMode) {
        url = "http://localhost:8080/" + "registerPlayer?name=" + name + "&mode=" + (singlePlayerMode ? 1 : 0);
        return restOperations.getForObject(url, int.class);
    }

    @Override
    public int registerPlayer(String name, boolean singlePlayerMode) {
        url = "http://localhost:8080/" + "registerPlayer/" + name + "/" + (singlePlayerMode ? 1 : 0);
        return restOperations.getForObject(url, int.class);
    }
}

RestConfig 代码

@Configuration
public class RestConfig {

    @Bean
    public RestOperations createRestTemplate(final ClientHttpRequestFactory clientHttpRequestFactory){
        return new RestTemplate(clientHttpRequestFactory);
    }

    @Bean
    public ClientHttpRequestFactory clientHttpRequestFactory(@Value("${connect.timeout}") final int connectTimeout, @Value("${read.timeout}") final int readTimeout){
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpComponentsClientHttpRequestFactory.setReadTimeout(readTimeout);
        httpComponentsClientHttpRequestFactory.setConnectTimeout(connectTimeout);
        return httpComponentsClientHttpRequestFactory;
    }
}

应用代码

@SpringBootApplication
public class App implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    private GameSessionClient gameSessionClient;

    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        int playerNr = gameSessionClient.registerPlayer("test", true);
        logger.info("Response: {}", playerNr);
    }
}

return restOperations.getForObject(url, int.class); 的结果是 java.lang.NullPointerException

url:http://localhost:8080/registerPlayer/test/1http://localhost:8080/registerPlayer?name=test&mode=1 在使用我的浏览器时都会导致 1

任何帮助将不胜感激,因为我对此感到很困惑。

【问题讨论】:

  • 请记住,这是一个非常常见的错误消息,其中包含很多关于如何解决的信息。
  • restOperations 未正确注入。再次检查。
  • 你能把createRestTemplate重命名为restOperations吗?
  • 将其重命名为 restOperations 无效。虽然我改变了:private RestOperations restOperations;private RestOperations restOperations = new RestConfig().createRestTemplate(new RestConfig().clientHttpRequestFactory(10000,10000)); 这是一个解决方法,但仍在试图弄清楚为什么它不是 AutoWiring

标签: java spring nullpointerexception get resttemplate


【解决方案1】:

将您的代码更新到下面..将删除您获得的 NullPointer 异常:

@Bean
    public RestOperations restOperations(final ClientHttpRequestFactory clientHttpRequestFactory){
        return new RestTemplate(clientHttpRequestFactory);
    }

并这样做,而不是“Application.class”:

public static void main(String[] args){
    SpringApplication.run(App.class, args);
}

【讨论】:

  • 如上所述,重命名它仍然给我一个NullPointerException
  • 更新了我的答案..你有 Application.class,而不是 App.class(以匹配类的名称)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2017-11-26
  • 1970-01-01
相关资源
最近更新 更多