不理解GetMapping()

问题描述 投票:0回答:1

我正在 JBoss 服务器上使用 Spring Boot 应用程序。我是 java、spring 和 JBoss 的新手。

我正在研究 https://www.javainuse.com/spring/springboot_session

的会话管理示例

我运行了这段代码:

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

当我运行它并转到 myurl/prototype1/ 时,我得到了一个空白页面。所以我将 GetMapping 更改为 /test...对此没有很好的解释,只是看起来像是值得尝试的事情

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

我将其部署到JBoss服务器上。

我回到 myurl/prototype1/ 并刷新以查看是否收到“未找到”错误而不是白页。令人惊讶的是,我得到了该页面...我原以为它已被移至 myurl/prototype1/test,但它位于 muyrl/prototype1。

此外,myurl/prototype1/test 显示空白页面。

我想双重确定我实际上正在运行我刚刚更改的代码,因此我在index.html 的标题后面添加了一个“2”并再次部署它

“2”出现了。

我还查看了 JBoss 的源代码,确信它部署正确:

它显示了 /test 映射...

我只能假设我只是不明白@GetMapping() 是如何工作的。谁能帮我纠正一下吗?

编辑:我也尝试将其更改为 @GetMapping("/asdf") ,但该页面仍然显示在 / 处,而 /asdf 为空白。这与使用index.html模板的方法有什么关系吗?这似乎没有意义..

java spring-boot jboss get-mapping
1个回答
0
投票

@GetMapping() 处理对任何 URI 路径的请求,因此如果您有 GetMapping("/test"),此函数或类将处理对 example.org/test 的 GET 请求。您还可以使用 @RequestMapping("/something") 注释类以添加路径,因此组合后将是 'example.org/something/test' 并将由 GetMapping 注释函数处理

© www.soinside.com 2019 - 2024. All rights reserved.