Backend Development 8 min read

Understanding the Four Types of Return Values in Spring MVC

This article explains the four possible return types of Spring MVC controller methods—ModelAndView, void (including no‑value, redirect, and server‑side forward), String (as view name, redirect, forward, or raw response), and JSON—providing code examples and usage guidelines for each scenario.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding the Four Types of Return Values in Spring MVC

Spring MVC is a fundamental Java web framework, and knowing the possible return types of controller methods is essential for both traditional and modern front‑end/back‑end separated architectures.

1. ModelAndView

Before front‑end separation, ModelAndView was the most common return type. It allows you to set a view name and bind data to the model.

@RequestMapping("/book")
public ModelAndView getAllBook() {
    ModelAndView mv = new ModelAndView();
    List
books = new ArrayList<>();
    Book b1 = new Book();
    b1.setId(1);
    b1.setName("三国演义");
    b1.setAuthor("罗贯中");
    books.add(b1);
    Book b2 = new Book();
    b2.setId(2);
    b2.setName("红楼梦");
    b2.setAuthor("曹雪芹");
    books.add(b2);
    mv.addObject("bs", books);
    mv.setViewName("book"); // specify view name
    return mv;
}

The two most common operations are setting the data model and specifying the view name.

2. Void

When a method returns void , it may truly have no data to return, or it may use other mechanisms to send a response.

2.1 No value

@RequestMapping("/test2")
@ResponseBody
public void test2() {
    // your code
}

2.2 Redirect

@RequestMapping("/test1")
@ResponseBody
public void test1(HttpServletResponse resp) {
    resp.setStatus(302);
    resp.addHeader("Location", "/aa/index");
}
// or using sendRedirect
public void test1(HttpServletResponse resp) {
    resp.sendRedirect("/aa/index");
}

2.3 Server‑side forward

@GetMapping("/test5")
public void test5(HttpServletRequest req, HttpServletResponse resp) {
    req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req, resp);
}

2.4 Write string directly

@RequestMapping("/test2")
@ResponseBody
public void test2(HttpServletResponse resp) throws IOException {
    resp.setContentType("application/json;charset=utf-8");
    PrintWriter out = resp.getWriter();
    List
books = new ArrayList<>();
    // populate books ...
    String s = new Gson().toJson(books);
    out.write(s);
    out.flush();
    out.close();
}

Even though the method returns void , data can still be sent to the client via the response object.

3. String

Returning a String can represent several different meanings.

3.1 Logical view name

@RequestMapping("/hello")
public String aaa(Model model) {
    model.addAttribute("username", "张三");
    return "hello"; // logical view name
}

3.2 Redirect

@RequestMapping("/test4")
public String test4() {
    return "redirect:/aa/index";
}

3.3 Forward

@RequestMapping("/test3")
public String test3() {
    return "forward:/WEB-INF/jsp/order.jsp";
}

3.4 Raw string response

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello provider!"; // returned directly as response body
    }
}
// or using @Controller with @ResponseBody on the method

4. JSON

Returning JSON is the most common case in a front‑end/back‑end separated project. Objects, lists, or maps are automatically converted to JSON by HttpMessageConverter (Jackson, Gson, etc.).

@GetMapping("/user")
@ResponseBody
public User getUser() {
    User user = new User();
    List
favorites = new ArrayList<>();
    favorites.add("足球");
    favorites.add("篮球");
    user.setFavorites(favorites);
    user.setUsername("zhagnsan");
    user.setPassword("123");
    return user;
}

@GetMapping("/users")
@ResponseBody
public List
getAllUser() {
    List
users = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        User e = new User();
        e.setUsername("zhangsan:" + i);
        e.setPassword("pwd:" + i);
        users.add(e);
    }
    return users;
}

Summary

The four return types— ModelAndView , void , String , and JSON—cover most scenarios you will encounter when developing Spring MVC applications. Choose the appropriate type based on whether you need to render a view, perform a redirect/forward, return raw data, or produce JSON for a front‑end client.

backendJavaWeb DevelopmentSpringMVCReturn Types
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.