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

相关推荐
唐老板8 小时前
前端和后端,转 AI 全栈怎么转
前端·后端
掘金安东尼8 小时前
中国移动 OAS 是什么:智能体开放服务空间、四项能力与「自研」说法的核实
人工智能·后端
JuiceFS8 小时前
缓存命中率 99.96%,小文件数量减少 99.92%:启智机器人 JuiceFS 生产实践
人工智能·后端·机器人
程序员鱼皮8 小时前
Antigravity 保姆级实战教程,从 0 到 1 万字干货!
前端·后端·ai编程
tong_li8 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(九):AI 分得清老大和老二(多用户隔离与 token 认证)
后端·langchain
AAA修煤气灶刘哥9 小时前
数据搬到另一朵云,小红书的训练任务怎么接着读?
数据库·后端·架构
SimonKing9 小时前
Typora 收费,开源产品Textus或可替代,还支持AI
java·后端·程序员
Rain的Java大神之路10 小时前
别再死背八股了:读完 Spring 7.0.8 源码,我学会了"改"框架
java·后端·面试
用户479492835691510 小时前
开源突破 10 万+ Star:Archify v3.3.2 深度拆解——超越 Mermaid 的离线架构诊断、1080p Story 视频与 PR 变更可视化
前端·后端·agent
挖掘狂人10 小时前
如何进行MySQL调优
数据库·后端·mysql