0

I am beginner in SpringBoot and MongoDB i tried to use GET method for Finding all my Players in MongoDB. my program works for all methods: PUT,POST,DELETE and GET("/{ID}) but it does not works for GET() I cannot understand where i made mistake, or what is the problem because i tried so many ways such as: changing the order and put the Get() getAllPlayers after getPlayerByID or i used @Get("/"), for this i recived Error 405. could you help me please?! my playerController is:

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

    }   

}
5
  • Please add the error you get to the question. Commented Apr 21, 2020 at 11:27
  • { "timestamp": "2020-04-21T11:28:21.734+0000", "status": 500, "error": "Internal Server Error", "message": "Query failed with error code 2 and error message 'Field 'locale' is invalid in: { locale: \"player\" }' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'Field 'locale' is invalid in: { locale: \"player\" }' on server localhost:27017", Commented Apr 21, 2020 at 11:28
  • There is an error in your log, check it and add it. MIght need to re-run with --debug enabled. Commented Apr 21, 2020 at 11:29
  • could you pleas see my code in Gitub: github.com/saharsahbaa/MyPlayers.git. i did not understand where is this log which you pointed Commented Apr 21, 2020 at 12:07
  • The query to your mongodb is wrong, which means you have a setup issue with MongoDB. See stackoverflow.com/questions/59532821/… Commented Apr 21, 2020 at 12:28

2 Answers 2

0

I found the problem Inside MongoDB, i have database which name is palayers and inside this, i have collection which name is playaer my mistake was, instead of writing @Document(Collection = "players"), i wrote @Document(colletion = "players") and becuase they seems more or less similar i did not pay attention at them. Thank you!

Sign up to request clarification or add additional context in comments.

Comments

-1

Change the order of the methods in your Controller

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

//Get All Players
@GetMapping
...

4 Comments

I used this, @GetMapping(value="/"), but nothing changed and i had this error: 405, "error": "Method Not Allowed"
no problem, In fact i tried so many ways, i changed the order but in both cases, with @Get("/") i have error 405, and with using just @Get() i have error 500. Nothing changed for me.
Changing the order won't matter. Spring will detect the proper one, ordering isn't important.
Really? Pls add a reference to the documentation so we all learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.