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

相关推荐
q***46521 小时前
在2023idea中如何创建SpringBoot
java·spring boot·后端
hygge9991 小时前
Spring Boot + MyBatis 整合与 MyBatis 原理全解析
java·开发语言·经验分享·spring boot·后端·mybatis
q***13611 小时前
十七:Spring Boot依赖 (2)-- spring-boot-starter-web 依赖详解
前端·spring boot·后端
q***25211 小时前
Spring Boot接收参数的19种方式
java·spring boot·后端
WX-bisheyuange1 小时前
基于Spring Boot的民谣网站的设计与实现
java·spring boot·后端
q***14641 小时前
Spring Boot文件上传
java·spring boot·后端
WX-bisheyuange3 小时前
基于Spring Boot的民宿预定系统的设计与实现
java·spring boot·后端·毕业设计
卷福同学4 小时前
【AI编程】用Codebuddy+lighthouse开发AI年龄模拟网站
javascript·后端
5***79004 小时前
后端API设计趋势,GraphQL与REST对比
后端·graphql
桜吹雪5 小时前
手搓一个简易Agent
前端·人工智能·后端