目录
1.请求映射路径
在类上面统一设置@RequestMapping注解,统一设置访问路径前缀。
在上文案例的基础上进行修改。
src/main/java/org/example/controller/UserController.java
package org.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/user")
public class UserController {
// 设置当前操作的访问路径
@RequestMapping("/save")
// 设置当前操作的返回值类型
@ResponseBody
public String save() {
System.out.println("user save...");
return "{'module':'springmvc'}";
}
@RequestMapping("/delete")
// 设置当前操作的返回值类型
@ResponseBody
public String delete() {
System.out.println("user delete...");
return "{'module':'user delete'}";
}
}
src/main/java/org/example/config/SpringMvcConfig.java
package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.example.controller")
public class SpringMvcConfig {
}
src/main/java/org/example/config/ServletContainerConfig.java
package org.example.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletContainerConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
2.普通请求参数的传递
src/main/java/org/example/controller/UserController.java
package org.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(String name, int age) {
System.out.println("参数 =>" + name + age);
return "{'module':'common param'}";
}
}
请求参数名和形参名不同,可以使用RequestParam()方法。
public String commonParam(@RequestParam("name") String name, @RequestParam("age") int age){
}
使用postman测试接口。
Get请求
Post请求
3.其它类型的参数的传递
①pojo类、json对象参数传参
src/main/java/org/example/controller/UserController.java
package org.example.controller;
import org.example.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/pojoParam")
@ResponseBody
public String pojoParam(User user) {
System.out.println("pojo类参数 =>" + user);
return "{'module':'pojo param'}";
}
@RequestMapping("/pojoParamForJson")
@ResponseBody
public String pojoParamForJson(@RequestBody User user) {
System.out.println("json对象参数 =>" + user);
return "{'module':'pojo for json param'}";
}
}
src/main/java/org/example/domain/User.java
package org.example.domain;
public class User {
private String name;
private int age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
json对象传参需要在pom.xml中额外导入jackson-databind依赖,并且在配置文件中使用EnableWebMvc注解,用于json数据的自动类型转换。
src/main/java/org/example/config/SpringMvcConfig.java
package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan("org.example.controller")
@EnableWebMvc
public class SpringMvcConfig {
}
②日期型参数传递
src/main/java/org/example/controller/UserController.java
package org.example.controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
@Controller
public class UserController {
@RequestMapping("/dateParam")
@ResponseBody
public String pojoParam(Date date,
@DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2) {
System.out.println("参数date =>" + date);
System.out.println("参数date1 =>" + date1);
System.out.println("参数date2 =>" + date2);
return "{'module':'date param'}";
}
}
4.响应
响应页面、文本数据、POJO对象
src/main/java/org/example/controller/UserController.java
package org.example.controller;
import org.example.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@RequestMapping("/toJumpPage")
public String toJumpPage(){
System.out.println("跳转页面");
return "page.jsp";
}
@RequestMapping("/toText")
@ResponseBody
public String toText(){
System.out.println("返回文本数据");
return "response text";
}
@RequestMapping("/toJsonPOJO")
@ResponseBody
public User toJsonPOJO(){
System.out.println("返回json对象数据");
User user = new User();
user.setAge(16);
user.setName("lisi");
return user;
}
}
src/main/webapp/page.jsp
<html>
<body>
<h2>This is page.jsp</h2>
</body>
</html>
其它
请求的url默认要有一个项目名称xxx的前缀,比如:http://localhost:8080/xxx/commonParam。想删掉这个前缀,需要配置一下,如下图: