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

相关推荐
Qiuner31 分钟前
Spring Boot AOP (六)架构落地与最佳实践
spring boot·后端·架构
计算机毕设VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vue在线考试系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Tony Bai7 小时前
Go 的 AI 时代宣言:我们如何用“老”原则,解决“新”问题?
开发语言·人工智能·后端·golang
用户47949283569158 小时前
性能提升 40 倍!实战 PostgreSQL FDW 解决微服务跨库查询难题
数据库·后端
计算机毕设VX:Fegn08959 小时前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Victor35610 小时前
Hibernate(9)什么是Hibernate的Transaction?
后端
Victor35610 小时前
Hibernate(10)Hibernate的查询语言(HQL)是什么?
后端
苏三说技术11 小时前
SpringSecurity、shiro 和 sa-token,到底选哪个?
后端
qq_27049009611 小时前
SpringBoot药品管理系统设计实现
java·spring boot·后端