1
2
获得rest-spring-mvc.zip
解压后,IDEA导入该项目。
在com.wsc.restspringmvc下创建pojo,然后创建Person类
package com.wsc.restspringmvc.pojo;
public class Person {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3
在com.wsc.restspringmvc下创建controller,然后创建PersonRestController
package com.wsc.restspringmvc.controller;
import com.wsc.restspringmvc.pojo.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonRestController {
@GetMapping("/person/{id}")
public Person person(@PathVariable Long id, @RequestParam(required = false) String name){
Person per=new Person();
per.setId(id);
per.setName(name);
return per;
}
}
4 运行RestSpringMvcApplication.java
浏览器输入:
http://localhost:8080/person/1?name=袅袅炊烟
结果:
{"id":1,"name":"袅袅炊烟"}
5
注意:
1.IDEA中Alt+4可切换到console