I am following up on a Full Stack Development Course provided by freeCodeCamp right here. Everything works just fine when I created a MovieController (Java class), MovieRepository (Java interface), and MovieService (Java class). I also connect succesfully my MongoDB into this project through instruction from the video as well. Here is the following code for 3 of the files above:
MovieController.java
package com.example.movies;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/v1/movies")
public class MovieController
{
@Autowired
private MovieService movieService;
@GetMapping
public ResponseEntity<String> getAllMovies()
{
return new ResponseEntity<String>("All movies", HttpStatus.OK);
}
}
MovieRepository.java
package com.example.movies;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface MovieRepository extends MongoRepository<Movie, ObjectId>
{
}
MovieService.java
package com.example.movies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MovieService
{
@Autowired
private MovieRepository movieRepository;
public List<Movie> allMovies()
{
return movieRepository.findAll();
}
}
However, when I changed the code in MovieController.java in the same way video instructs me like below:
package com.example.movies;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/api/v1/movies")
public class MovieController
{
@Autowired
private MovieService movieService;
@GetMapping
public ResponseEntity<List<Movie>> getAllMovies()
{
return new ResponseEntity<List<Movie>>(movieService.allMovies(), HttpStatus.OK);
}
}
It gives me a following error in the command prompt:
enter image description here
I have researched some solution on the Internet, but I always came up with the solution that I need to check my URL in @RequestMapping. Although I find it is correct, the command prompt keeps telling me that there is invalid syntax in my URL. Is there any solution for this kind of problem? I appreciate for any help and support. Thank you.
Trần Khôi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.