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

相关推荐
长安不见几秒前
图与循环依赖、死锁(一) 为何我的循环依赖时好时坏?
后端
codervibe1 分钟前
Spring Boot 项目中泛型统一响应封装的设计与实现
后端
阿华的代码王国23 分钟前
【Android】日期选择器
android·xml·java·前端·后端
hqxstudying44 分钟前
SpringBoot相关注解
java·spring boot·后端
Livingbody2 小时前
ubuntu25.04完美安装typora免费版教程
后端
阿华的代码王国2 小时前
【Android】RecyclerView实现新闻列表布局(1)适配器使用相关问题
android·xml·java·前端·后端
码农BookSea2 小时前
自研 DSL 神器:万字拆解 ANTLR 4 核心原理与高级应用
java·后端
lovebugs3 小时前
Java并发编程:深入理解volatile与指令重排
java·后端·面试
海奥华23 小时前
操作系统到 Go 运行时的内存管理演进与实现
开发语言·后端·golang
codervibe3 小时前
Spring Boot 服务层泛型抽象与代码复用实战
后端