SpringMVC Restful风格

在controller的类上添加@RestController注解

java 复制代码
package com.xiaoya.controller;

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

@Controller
@RestController
public class TestController {
    @RequestMapping("/hello/{name}")
    @ResponseBody
    public String hello(@PathVariable String name){
        return "hello "+name;
    }
}

在方法的参数前面标上@PathVariable注解,然后路径中使用{name}就可以解析变量

启动tomcat,访问http://localhost:8080/springmvc1_war_exploded/hello/2

相关推荐
小刘|几秒前
Spring AI 结构化输出 + 大模型参数全解(含千问调优)
java·后端·spring
copyer_xyf3 分钟前
FastAPI 项目骨架搭建
前端·后端·python
laowangpython14 分钟前
tokio-rstracing:Rust 可观测性的标准答案
开发语言·后端·其他·rust
IT_陈寒16 分钟前
Python虚拟环境的这个坑,我居然绕了三天才爬出来
前端·人工智能·后端
我登哥MVP18 分钟前
SpringCloud 核心组件解析:服务熔断和降级
java·spring boot·后端·spring·spring cloud·java-ee·maven
Oneslide23 分钟前
Claude Code 插件完全指南:安装与技能大全
后端
摇滚侠27 分钟前
SpringMVC 入门到实战 异常处理 83-85
java·后端·spring·maven·intellij-idea
TPBoreas37 分钟前
springboot我们项目中的常见注解
java·spring boot·后端
霸道流氓气质1 小时前
JWT 认证全面解析:原理、流程与 Spring Boot 实战
java·spring boot·后端
王小王-1231 小时前
基于Django的个性化餐饮场所推荐系统
后端·python·django·个性化餐厅推荐·个性化餐饮推荐