Mihály Varga Mihály Varga - 2 months ago 18
reST (reStructuredText) Question

Using Jparepository's FindAll method with ModelAndView

My UserController class:

package org.peronalitymeet.controller;

import org.peronalitymeet.entity.User;
import org.peronalitymeet.entity.request.AddUserRequest;
import org.peronalitymeet.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

private UserRepository userRepository;

@Autowired
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}

@RequestMapping(value="/users", method = RequestMethod.GET)
public List<User> findAllUsers(){
return userRepository.findAll();
}

@RequestMapping(value="/signup", method = RequestMethod.POST)
public void addUser(@RequestBody AddUserRequest addUserRequest){
User user = new User();
user.setFirstname(addUserRequest.getFirstname());
user.setSurname(addUserRequest.getSurname());
userRepository.save(user);
}

}


My problem is that now if i open the localhost:8080/users URL,
i will get all my users within JSON format. Previously i followed a Spring mvc tutorial, and there they used ModelAndView method to automatically open a jsp page if an URL was called.

Is there any way to use FindAll Users() and at the same time open a jsp or html page and list the result of the method within that?

Answer

@RestController is a meta-annotation that tells Spring to return the text that it gets back as-is to the user. It's equivalent to adding @ResponseBody @Controller to your class.

If you want to do something with a JSP, your class should look something like what I have below. I am only changing the /users endpoint, but the idea would apply everywhere.

package org.peronalitymeet.controller;

import org.peronalitymeet.entity.User;
import org.peronalitymeet.entity.request.AddUserRequest;
import org.peronalitymeet.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@Controller
public class UserController {

    private UserRepository userRepository;

    @Autowired
    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @RequestMapping(value="/users", method = RequestMethod.GET)
    public ModelAndView findAllUsers(ModelAndView mav){
        List<User> users = userRepository.findAll();
        mav.addObject("users", users);
        mav.setViewName("users");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value="/signup", method = RequestMethod.POST)
    public void addUser(@RequestBody AddUserRequest addUserRequest){
        User user = new User();
        user.setFirstname(addUserRequest.getFirstname());
        user.setSurname(addUserRequest.getSurname());
        userRepository.save(user);
    }

}

Then set up your view page called users.jsp to display the items in the users variable and you should be set.