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 小时前
Hippo4j 线程池监控平台部署手册
java·spring boot·后端
万少10 小时前
AtomCode开发微信小程序《谁去呀》 全流程
前端·javascript·后端
GetcharZp10 小时前
Epic、暴雪都在用的 C++ 界面利器:Dear ImGui 零基础全景指南
后端
pixcarp11 小时前
知识库系统的内容资产闭环怎么设计
服务器·数据库·后端·golang
红尘散仙11 小时前
别再手动录屏了:用 VHS 给终端应用生成会动的文档素材
后端·rust
张忠琳14 小时前
【Go 1.26.4】Golang Select 深度解析
开发语言·后端·golang
IT_陈寒15 小时前
React中useEffect依赖项这个坑我居然踩了三天
前端·人工智能·后端
提笔了无痕15 小时前
如何用Go实现整套RAG流程
开发语言·后端·golang
成都第一深情IZZO16 小时前
事务未提交就发送 MQ,导致消费者读不到订单数据的问题
后端
大橙子打游戏16 小时前
Fable5不能用了,但是依然能让 AI 纯靠截图玩通宝可梦
后端