简体   繁体   English

在Java中使用Spring RestTemplate时发生NullPointedException

[英]NullPointedException when using Spring RestTemplate in Java

I'm using the latest Springframework, and having issues trying to GET an int from my server. 我正在使用最新的Springframework,尝试从服务器获取int时遇到问题。 All code was writen in Java. 所有代码都是用Java编写的。

When I interact with the server throught browser everything is OK. 当我通过浏览器与服务器交互时,一切正常。 And when interacting with the server through the client I'm getting a NullPointerException . 当通过客户端与服务器交互时,我得到了NullPointerException

Keep in mind I am a beginner software student. 请记住,我是一名初学软件的学生。

Server Code (I tried both, works fine when using browser): 服务器代码 (我都尝试过,在使用浏览器时工作正常):

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);
    }

}

Client Code (again tried both, with the same result): 客户端代码 (再次尝试两者,结果相同):

@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 Code : 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;
    }
}

App Code : 应用程式码

@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);
    }
}

The return restOperations.getForObject(url, int.class); return restOperations.getForObject(url, int.class); results in a java.lang.NullPointerException 导致java.lang.NullPointerException

url: http://localhost:8080/registerPlayer/test/1 or http://localhost:8080/registerPlayer?name=test&mode=1 both result in 1 when using my browser 网址: http://localhost:8080/registerPlayer/test/1http://localhost:8080/registerPlayer?name=test&mode=1都在使用我的浏览器时导致1

Any help would be much appreciated, as I'm getting pretty confused from this. 任何帮助将不胜感激,因为我对此感到非常困惑。

Update you code to below..will remove the NullPointer Exception your getting: 将您的代码更新到下面。.将删除您得到的NullPointer异常:

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

and do this , instead of "Application.class": 并执行此操作,而不是“ Application.class”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM