Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Spring Basics Using the MVC Architecture Fix a Controller

Bummer! Think about how the Spring Framework is going to initially recognize this class as a controller.

Fix the one-line mistake in the class below to restore your application's functionality.

There is only one class: "public class TaskController" (which would seem to make it easy).


The question that immediately came to mind:

"So does the fix go inside the class or attached to the outside the class?"

I googled a lot and found most of the Spring boot code samples were using '@RestController'

However neither this nor variations of @Rest*something* seem to work.


So I stopped googling, and went back to looking at the "CategoryController.java" in the courses 'giflib' zip download and at time index 09:40 of this video:

https://teamtreehouse.com/library/spring-basics/using-the-mvc-architecture/add-a-categorycontroller


I spend a long long time sitting there just staring at the code line by line.

Finally it hit me --I saw they were using '@Controller' before the 'public class TaskController' line.

Could it be that simple (was I over thinking things..):

package com.teamtreehouse.todo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.ui.ModelMap;

import com.teamtreehouse.todo.repository.TaskRepository;
import com.teamtreehouse.todo.model.Task;

@Controller
public class TaskController {
  @Autowired
  private TaskRepository taskRepository;

  @RequestMapping("/")
  public String home() {
    return "task_index";
  }

  @RequestMapping("/tasks/{id}")
  public String task(@PathVariable int id, ModelMap modelMap) {
    modelMap.put("task",taskRepository.findById(id));
    return "task_detail";
  }
}

Unbelievable - it passed!

It wouldn't have taken me weeks to figure out if they had just said:

"Please add the missing single line @ annotation needed to fix the 404 error."

..and left out the confusing Bummer! 'clue' (hint) about class controller recognition