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

相关推荐
问道飞鱼2 小时前
【Rust编程语言】Rust数据类型全面解析
开发语言·后端·rust·数据类型
泉城老铁3 小时前
目前开源架构需要注意的安全问题
spring boot·后端
ZoeGranger3 小时前
【Spring】IoC 控制反转、DI 依赖注入、配置文件和bean的作用域
后端
马卡巴卡3 小时前
分库分表数据源ShardingSphereDataSource的Connection元数据误用问题分析
后端
superman超哥3 小时前
仓颉动态特性探索:反射API的原理、实战与性能权衡
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·仓颉动态特性·反射api
骑着bug的coder3 小时前
第7讲:索引(下)——失效场景与优化实战
后端·mysql
superman超哥3 小时前
仓颉元编程之魂:宏系统的设计哲学与深度实践
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·仓颉语言特性
一 乐3 小时前
健身房预约|基于springboot + vue健身房预约小程序系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·小程序
踏浪无痕4 小时前
JobFlow:时间轮与滑动窗口的实战优化
后端·架构·开源
molaifeng4 小时前
像搭积木一样理解 Golang AST
开发语言·后端·golang