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

相关推荐
象牙塔五六七15 分钟前
Java 泛型精讲:从 Stream 源码说起
后端
掘金挖土16 分钟前
前端手摸手跑路之 AI 应用开发(十二)
前端·后端
程序员cxuan17 分钟前
GPT-6.2 Astra 发布了,但口碑崩了。。。
人工智能·后端·程序员
象牙塔五六七18 分钟前
Java 函数式编程精讲:从 Stream 源码说起 —— 用真实源码学懂函数式思维
后端
字节跳动数据库32 分钟前
火山引擎缓存数据库:如何构建超大规模缓存集群的无感弹性能力
数据库·人工智能·后端
IT枫斗者枫哥33 分钟前
Java 异步导出只测最终行数,为什么下载时仍会少一行?
java·后端
用户78506119579734 分钟前
不会前端,我靠 AI 和 Python 做了一个 4K 壁纸站
后端
噜噜学上位机35 分钟前
什么是多态?
后端
站大爷IP38 分钟前
Python的类方法把我坑惨了,原来@classmethod和@staticmethod的区别这么大
后端
Gopher_HBo44 分钟前
StartCPUProfile 秒返回,是谁把 cpu.pprof 写满的?
后端