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

相关推荐
IT_陈寒34 分钟前
SpringBoot自动配置坑了我一周,原来问题这么蠢!
前端·人工智能·后端
iOS开发上架哦2 小时前
Android代码混淆与iOS加固技术详解
后端·ios
Alan_6912 小时前
商品详情优化三板斧-拆分-多级缓存-GC调参
后端·缓存
Conan在掘金2 小时前
ArkTS 进阶之道(3):为哈禁解构声明?类型一眼可见 vs 推断链断裂
后端
晚安code2 小时前
干掉成山的 if-else:工厂造、策略选,一文讲透两个模式的配合
后端·设计模式
feng尘2 小时前
深度解析布隆过滤器(Bloom Filter):原理、优缺点与 1000 万黑名单实战
后端·面试
大陈AI2 小时前
Docker Compose 前后端部署踩坑实录:3 个坑让我的容器反复 Exit(1)
后端
长大19882 小时前
MySQL 慢查询排查完整流程
后端
苏三说技术3 小时前
为什么越来越多人使用FastAPI?
后端
老孙讲技术3 小时前
业主半夜想看楼道监控,物业却说「去机房」?我用设备托管+轻应用,把小区摄像头嵌进了社区小程序
后端·物联网