Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Get()方法错误500,使用SpringBoot和MongoDB时出现内部服务器错误_Java_Mongodb_Spring Boot_Spring Mvc_Postman - Fatal编程技术网

Java Get()方法错误500,使用SpringBoot和MongoDB时出现内部服务器错误

Java Get()方法错误500,使用SpringBoot和MongoDB时出现内部服务器错误,java,mongodb,spring-boot,spring-mvc,postman,Java,Mongodb,Spring Boot,Spring Mvc,Postman,我是SpringBoot和MongoDB的初学者 我尝试使用GET方法在MongoDB中查找我的所有玩家。 我的程序适用于所有方法:PUT、POST、DELETE和GET(“/{ID}),但不适用于GET() 我无法理解我在哪里犯了错误,或者问题出在哪里,因为我尝试了很多方法,例如:更改顺序并将Get()getAllPlayers放在getPlayerByID之后,或者我使用了@Get(“/”),因此我收到了错误405。 你能帮帮我吗?! 我的playerController是: package

我是SpringBoot和MongoDB的初学者 我尝试使用GET方法在MongoDB中查找我的所有玩家。 我的程序适用于所有方法:PUT、POST、DELETE和GET(“/{ID}),但不适用于GET() 我无法理解我在哪里犯了错误,或者问题出在哪里,因为我尝试了很多方法,例如:更改顺序并将Get()getAllPlayers放在getPlayerByID之后,或者我使用了@Get(“/”),因此我收到了错误405。 你能帮帮我吗?! 我的playerController是:

package thesisMongoProject.controller;    

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import thesisMongoProject.Player;
import thesisMongoProject.Repository.PlayerRepository;

@RestController
@RequestMapping("/player")
public class PlayerController {
    @Autowired
    private PlayerRepository repo;

    //Get All Players
    @GetMapping
    public List<Player> getAllPlayers() {
        return repo.findAll();

    }

    //Getting Player ID
    @GetMapping("/{nickname}")
    public Player getPlayerByID(@PathVariable String nickname){
        return repo.findById(nickname).get();
        }





    //Delete Players
    @DeleteMapping
    public String deleteAllPlayers(){
        repo.deleteAll();
        return "Deleted!";      
    }

    //Create Player
    @PostMapping
    public ResponseEntity<?> createPlayer(@RequestBody Player player){

                repo.save(player);
                return ResponseEntity.status(201).body("Created!");


    }

    //Delete player By ID
    @DeleteMapping("/{nickname}")
    public ResponseEntity<?> deletePlayerByID(@PathVariable String nickname){
        try {

            Player p = repo.findById(nickname).get();
            return ResponseEntity.ok(p);

        } catch (Exception e) {
            return ResponseEntity.status(404).body("Not Found!");
        }
    }

    //Update Player By ID
    @PutMapping("/{nickname}")
    public ResponseEntity<?> updatePlayerByID(
            @PathVariable("nickname")String nickname,
            @RequestBody Player player){

        try {
            player.setNickname(nickname);
            repo.save(player);
            return ResponseEntity.ok(player);

        } catch (Exception e) {
            return ResponseEntity.status(404).body("Not Found!");
        }

    }   

}
将ISMONGOPROJECT.controller打包;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.DeleteMapping;
导入org.springframework.web.bind.annotation.GetMapping;
导入org.springframework.web.bind.annotation.PathVariable;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.bind.annotation.PutMapping;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入ISMONGOPROJECT.Player;
导入IsMongOproject.Repository.PlayerRepository;
@RestController
@请求映射(“/player”)
公共类播放控制器{
@自动连线
私募股权回购;
//让所有球员都上场
@GetMapping
公共列表GetAllPlayer(){
返回回购findAll();
}
//获取玩家ID
@GetMapping(“/{昵称}”)
公共播放器getPlayerByID(@PathVariable字符串昵称){
返回repo.findById(昵称).get();
}
//删除玩家
@删除映射
公共字符串deleteAllPlayers(){
repo.deleteAll();
返回“已删除!”;
}
//创建玩家
@邮戳
public ResponseEntity createPlayer(@RequestBody-Player){
回购保存(玩家);
返回ResponseEntity.status(201).body(“已创建”);
}
//按ID删除玩家
@DeleteMapping(“/{昵称}”)
公共响应属性deletePlayerByID(@PathVariable字符串昵称){
试一试{
Player p=repo.findById(昵称).get();
返回响应度ok(p);
}捕获(例外e){
返回ResponseEntity.status(404).body(“未找到”);
}
}
//按ID更新播放器
@PutMapping(“/{昵称}”)
公共响应性更新playerbyid(
@PathVariable(“昵称”)字符串昵称,
@请求体播放器(播放器){
试一试{
player.set昵称(昵称);
回购保存(玩家);
返回响应度。ok(玩家);
}捕获(例外e){
返回ResponseEntity.status(404).body(“未找到”);
}
}   
}

更改控制器中方法的顺序

//Getting Player ID
@GetMapping("/{nickname}")
....

//Get All Players
@GetMapping
...
我发现了问题 在MongoDB中,我有一个名为palayers的数据库,在这个数据库中,我有一个名为playaer的集合 我的错误是,我没有写@Document(Collection=“players”),而是写了@Document(Collection=“players”) 因为它们看起来差不多,我没有注意它们。
谢谢!

我使用了这个@GetMapping(value=“/”),但没有任何改变,我有一个错误:405,“error”:“Method Not Allowed”没问题,事实上我尝试了很多方法,我改变了顺序,但在这两种情况下,使用@Get(“/”)我都有错误405,而使用@Get()我有错误500。对我来说没有任何更改。更改订单无关紧要。Spring会检测到正确的订单,订单并不重要。真的吗?请添加文档参考以便我们都了解请添加问题的错误。{“时间戳”:“2020-04-21T11:28:21.734+0000”,“状态”:500,“错误”:“内部服务器错误”,“消息”:“查询失败,错误代码为2,错误消息‘字段’locale’在服务器localhost:27017上的:{locale:\'player\'}中无效;嵌套异常为com.mongodb.MongoQueryException:查询失败,错误代码为2,错误消息‘字段’locale’在服务器localhost:27017上的:{locale:\'player\'}中无效”,您的日志中有错误,请检查并添加它。可能需要在启用
--debug
的情况下重新运行。是否可以在Gitub中查看我的代码:。我不明白您指向mongodb的日志在哪里。对mongodb的查询错误,这意味着您的mongodb存在安装问题。请参阅