Java Can';使用spring boot时无法访问REST API

Java Can';使用spring boot时无法访问REST API,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,所以我刚刚开始使用SpringBoot,我尝试实现一个crud服务。我的代码主要来自本教程,我只是更改了变量名。 我解决了启动spring boot等的所有问题,但现在我无法访问本地主机上的api。我希望有人能帮我:) My Application.java package demo.app; 导入org.springframework.boot.SpringApplication; 导入org.springframework.boot.autoconfigure.springboot应用程序;

所以我刚刚开始使用SpringBoot,我尝试实现一个crud服务。我的代码主要来自本教程,我只是更改了变量名。 我解决了启动spring boot等的所有问题,但现在我无法访问本地主机上的api。我希望有人能帮我:)

My Application.java

package demo.app;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
@SpringBootApplication(scanBasePackages={“demo”})
公共类应用程序{
公共静态void main(字符串[]args){
SpringApplication.run(App.class,args);
}
}
My model Game.java:

包demo.model;
公开课游戏{
私人长id;
私有字符串名称;
私人弦乐体裁;
私人弦乐工作室;
私有字符串发布器;
公共游戏(长id、字符串名称、字符串类型、字符串工作室、字符串发行商){
this.id=id;
this.name=名称;
这个。流派=流派;
this.studio=studio;
this.publisher=publisher;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getgreen(){
回归体裁;
}
公共类型(字符串类型){
这个。流派=流派;
}
公共字符串getStudio(){
回归演播室;
}
公共void setStudio(字符串工作室){
this.studio=studio;
}
公共字符串getPublisher(){
返回出版商;
}
public void setPublisher(字符串发布器){
this.publisher=publisher;
}
@凌驾
公共字符串toString(){
return“Game[id=“+id+”,name=“+name+”,genre=“+genre+”,studio=“+studio+”,publisher=”
+publisher+“]”;
}
}
Api控制器RestApiController.java

包demo.app.controller;
导入java.util.List;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.HttpHeaders;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.util.UriComponentsBuilder;
导入demo.model.Game;
导入demo.service.GameService;
导入demo.util.CustomErrorType;
@RestController
@请求映射(“/api”)
公共类重启控制器{
公共静态最终记录器Logger=LoggerFactory.getLogger(RestApiController.class);
@自动连线
GameService GameService;//将执行所有数据检索/操作工作的服务
//---------------检索所有游戏---------------------------------
@RequestMapping(value=“/game/”,method=RequestMethod.GET)
公众响应列表所有游戏(){
List games=gameService.findAllGames();
if(games.isEmpty()){
返回新的响应属性(HttpStatus.NO_内容);
//TODO:HttpStatus.NOT_FOUNDändern中的vllt
}
返回新的响应状态(games,HttpStatus.OK);
}
//---------------检索单个游戏---------------------------------
@RequestMapping(value=“/game/{id}”,method=RequestMethod.GET)
公共响应属性getGame(@PathVariable(“id”)长id){
info(“正在获取id为{},id为的用户”);
Game Game=gameService.findById(id);
如果(游戏==null){
logger.error(“找不到id为{}的游戏。”,id);
返回新的ResponseEntity(新的客户错误类型(“id为“+id+”的游戏未找到”)、HttpStatus.not_found;
}
返回新的ResponseEntity(游戏,HttpStatus.OK);
}
//---------------创建一个游戏---------------------------------
@RequestMapping(value=“/game/”,method=RequestMethod.POST)
公共响应Entity createGame(@RequestBody Game,UriComponentsBuilder ucBuilder){
info(“创建游戏:{}”,游戏);
if(gameService.isGameExist(游戏)){
logger.error(“无法创建。名为{}的游戏已存在。”,Game.getName());
返回新的ResponseEntity(新的CustomerRorType(“无法创建名为“+Game.getName()+”的游戏已存在”)、HttpStatus.CONFLICT);
}
gameService.saveGame(游戏);
HttpHeaders=新的HttpHeaders();
headers.setLocation(ucBuilder.path(“/api/game/{id}”).buildAndExpand(game.getId()).toUri());
返回新的ResponseEntity(标题,HttpStatus.CREATED);
}
}
服务接口GameService.java:

package demo.service;
导入java.util.List;
导入demo.model.Game;
公共接口游戏服务{
游戏findById(长id);
游戏findByName(字符串名称);
无效保存游戏(Game-Game);
void updateGame(游戏);
void deleteGameByName(字符串名称);
列出findAllGames();
void deleteAllGames();
布尔isGameExist(游戏);
}
it GameServiceImpl.java的实现:

package demo.service;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入java.util.concurrent.AtomicLong;
导入org.springframework.stereotype.Service;
导入demo.model.Game;
@服务(“游戏服务”)
公共类GameServiceImpl实现GameService{
私有静态最终AtomicLong计数器=新AtomicLong();
私人静态列表游戏;
静止的{
games=populateDummyGames();
}
公共列表findAllGames(){
返回游戏;
}
公共游戏findById(长id){
用于(游戏:游戏){
if(game.getId()==id){
回归游戏;
}
}
返回null;
}
公共游戏findByName(字符串名称){
用于(游戏:游戏){
if(game.getName().equalsIgnoreCase(name)){
回归游戏;
}
}
返回null;
}
公共无效保存游戏(游戏){
游戏设置ID(计数器)
@SpringBootApplication(scanBasePackages= {"demo"})
@SpringBootApplication(scanBasePackages= {"demo.app"})
@SpringBootApplication
@EnableAutoConfiguration