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 小时前
DataSyncManager 详解与 Spring Boot 迁移指南
java·spring boot·后端
计算机程序设计小李同学2 小时前
个人数据管理系统
java·vue.js·spring boot·后端·web安全
Echo娴2 小时前
Spring的开发步骤
java·后端·spring
追逐时光者3 小时前
TIOBE 公布 C# 是 2025 年度编程语言
后端·.net
Victor3563 小时前
Hibernate(32)什么是Hibernate的Criteria查询?
后端
Victor3563 小时前
Hibernate(31)Hibernate的原生SQL查询是什么?
后端
_UMR_3 小时前
springboot集成Jasypt实现配置文件启动时自动解密-ENC
java·spring boot·后端
程序员小假4 小时前
我们来说说 Cookie、Session、Token、JWT
java·后端