Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Spring Basics Creating Controllers and Views Create a Controller to Handle HTTP Requests

Creating my own method from example. Not sure how to make another method that works with localhost:8080/gif

I tried making another class file (gifController example) but anything I do just doesn't let build be executed (errors on bootRun.) Can I ask how somebody did this at the end of the lesson? I'm also specifically curious about how to get it to link to localhost:8080/gif

Thanks

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

Hi Julya.

You shouldn't be creating a new class for this, you just need to add another method in the same class. The method name can be anything you want, I chose listGifDetails, and the only additional change should be the value in the @RequestMapping annotation, so it should be @RequestMapping("/gif"), meaning we want this method to capture HTTP request to that particular URI /gif. For making sure it all works, just make it return another String message, I chose "Details of a particular GIF".

This is how my GifController class looks after adding that method:

package com.bobantalevski.giflib.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GifController {

    @RequestMapping("/")
    @ResponseBody
    public String listGifs() {
        return "List of all the GIFs";
    }

    @RequestMapping("/gif")
    @ResponseBody
    public String listGifDetails() {
        return "Details of a particular GIF";
    }
}