Kmus
Kmus

Reputation: 53

Not Understanding GetMapping()

I'm working with a Spring Boot application on a JBoss server. I'm new to java, spring and JBoss.

I was working through a session management example from https://www.javainuse.com/spring/springboot_session

I ran this code:

package private.prototype1;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SpringSessionController {

    @GetMapping("/")
    public String process(Model model, HttpSession session) {
        @SuppressWarnings("unchecked")
        List<String> messages = (List<String>) session.getAttribute("MY_SESSION_MESSAGES");

        if (messages == null) {
            messages = new ArrayList<>();
        }
        model.addAttribute("sessionMessages", messages);

        return "index";
    }

    @PostMapping("/persistMessage")
    public String persistMessage(@RequestParam("msg") String msg, HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<String> messages = (List<String>) request.getSession().getAttribute("MY_SESSION_MESSAGES");
        if (messages == null) {
            messages = new ArrayList<>();
            request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
        }
        messages.add(msg);
        request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
        return "redirect:/";
    }

    @PostMapping("/destroy")
    public String destroySession(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/";
    }
}

When I ran it and went to myurl/prototype1/ I got a blank page. So I changed the GetMapping to /test... no great explanation for that, just seemed like something to try

package private.prototype1;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SpringSessionController {

    @GetMapping("/test")
    public String process(Model model, HttpSession session) {
        @SuppressWarnings("unchecked")
        List<String> messages = (List<String>) session.getAttribute("MY_SESSION_MESSAGES");

        if (messages == null) {
            messages = new ArrayList<>();
        }
        model.addAttribute("sessionMessages", messages);

        return "index";
    }

    @PostMapping("/persistMessage")
    public String persistMessage(@RequestParam("msg") String msg, HttpServletRequest request) {
        @SuppressWarnings("unchecked")
        List<String> messages = (List<String>) request.getSession().getAttribute("MY_SESSION_MESSAGES");
        if (messages == null) {
            messages = new ArrayList<>();
            request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
        }
        messages.add(msg);
        request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
        return "redirect:/";
    }

    @PostMapping("/destroy")
    public String destroySession(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/";
    }
}

I deployed it to the JBoss server.

I went back to myurl/prototype1/ and refreshed to see if I got a Not Found error instead of the white page. Surprisingly I'm getting the page... I expected it to have been moved to myurl/prototype1/test but instead its at muyrl/prototype1.

Also, myurl/prototype1/test shows a blank page.

I wanted to be double sure I was actually running the code I just changed so I added a "2" after the header in index.html and deployed it again

enter image description here

The "2" showed up.

I also viewed the source code from JBoss to be positive it deployed correctly:

enter image description here

It shows the /test mapping...

I can only assume that I just don't understand how @GetMapping() works. Can anyone straighten me out?

EDIT: I also just tried changing it to @GetMapping("/asdf") and still that page shows up at / and /asdf is blank. Does it have something to do with that method using the index.html template? That doesn't seem like it would make sense..

Upvotes: -1

Views: 75

Answers (1)

user21476112
user21476112

Reputation:

@GetMapping() handles requests to whatever URI path, so if you have GetMapping("/test") this function or class will handle GET requests to example.org/test. You can also annotate classes with @RequestMapping("/something") to prepend a path, so combined will be 'example.org/something/test' and will get handled by the GetMapping annotated function

Upvotes: 0

Related Questions