在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