【发布时间】: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/1 或 http://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