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 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";
    }
}