Mohammad Shoman
Mohammad Shoman

Reputation: 33

issue with springboot project : org.thymeleaf.exceptions.TemplateInputException

hey guys
i have a problem in my java spring boot application
i've built a simple application and connected it with a database
but when i try to make a POST or GET on the data base my program access the database and do any thing i did but show an error
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Students", template might not exist or might not be accessible by any of the configured Template Resolvers

when i make GET i check the Iterable list and it's already get the data from database but doesn't show the data on the localhost it's give me that exception is there any solution for that ? this is my code in controller

@Path("Students")
@Controller
public class studentsController {
    @AutoWired
    StudentServices st;
    @RequestMapping(method = RequestMethod.GET)
    public Iterable<Students> getAllStudents() {
          Iterable<Students> list = st.getAllStudents();
          return list
}

Upvotes: 1

Views: 646

Answers (1)

rieckpil
rieckpil

Reputation: 12051

With @Controller you are defining a Model-View-Controller (MVC) endpoint for returning your view templates. So with Iterable<Students> Spring is looking for a Students template in your src/main/resources/templates folder because it is interpreted as a View name.

If you want to create a REST endpoint which returns a list of Student objects you should use @RestController at your class which adds the Spring annotation @RequestBody automatically.

Furthermore @Path("XYZ") should be replaced with @RequestMapping("XYZ") in Spring and @AutoWired with @Autowired.

An working example could look like the following:

@RequestMapping("/students")
@RestController
public class StudentsController {

    @Autowired
    StudentServices st;

    @RequestMapping(value="/", method = RequestMethod.GET)
    public Iterable<Students> getAllStudents() {
          Iterable<Students> list = st.getAllStudents();
          return list
}

Upvotes: 1

Related Questions