kim
kim

Reputation: 143

@GetMapping method not calles

I am a beginner so please don't be mean. I have got an html page index.html And I want the method MainController::getListEmployee to be called. In this method, I put a System.err to see if the method is called. And I see nothing.

Controller code

package com.cgi.listeemployes.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cgi.listeemployes.model.User;
import com.cgi.listeemployes.repository.UserRepository;

@Controller // This means that this class is a Controller
@RequestMapping(path="/") // This means URL's start with /demo (after Application path)
public class MainController {
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path="/index.html")
    public @ResponseBody Iterable<User> getListEmployee() {
        // This returns a JSON or XML with the users
        System.err.println("getting ");
        return userRepository.findAll();
    }

    @PostMapping(path="/add") // Map ONLY POST Requests
    public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) {
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() {
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    }
}

thanks for your help

enter image description here

Upvotes: 3

Views: 678

Answers (1)

When you want to return a html, just return a string with the name of the html file, it could be "Index" (without the .html).

In your @GetMapping(path="/index.html"), you are returning an object instead a html.

If you want to load data from database and render it at your html, then add the attribute "Model model" in your parameters, like this:

@GetMapping(path="/index.html")
public String getListEmployee(Model model) {
    List<User> users = userRepository.findAll();
    model.addAttribute("yourUsers", users); // this gonna inject the list of users in your html
    System.err.println("getting ");
    return "Index"
}

Then in your html, you can get the users with ${yourUsers} and do whatever you want.

I saw your project, it is missing the template engine. Template engine is what gonna get the data of your backend and show in your front/html. I added the Thymeleaf template engine into your pom.xml, and it worked. Here is the thymeleaf dependency:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

To work with thymeleaf, you have to put all your html into a new folder called "templates" in the "resources", same level of "static". You cannot use html in the static folder, this folder should have only css, javascripts and assets.

Upvotes: 1

Related Questions