mvc接收各种请求
1-环境搭建
1.1-准备apifox发送请求
1.2-项目搭建
①创建Web骨架的Maven项目
--打开2023-IDEA ,选择New Project
--选择Maven Archetype
--注意点:Catalog默认就行了
--Archetype选择webapp
--JDK跟着黑马敲最好用11
②创建好以上包结构
③导入pom.xml坐标
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>provided</scope>
</dependency>
</dependencies>
④导入各种类,接口等
--config配置类
//ServletContainersInitConfig
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter charactFilter = new CharacterEncodingFilter();
charactFilter.setEncoding("UTF-8");
return new Filter[]{charactFilter};
}
}
//SpringConfig
@Configuration
@ComponentScan(value = "com.bdqn",
excludeFilters = @ComponentScan.Filter
(type = FilterType.ANNOTATION, classes = Controller.class
)) //排除注解类型,排除Controller.class
public class SpringConfig {
}
//SpringMvcConfig
@Configuration //标识这是一个配置类
@ComponentScan("com.bdqn.controller") //对指定范围进行扫包,对容器注册Bean对象
@EnableWebMvc //@EnableWebMvc 的作用是将当前的配置类标识为 Spring MVC 的配置类,并通过自动配置和默认功能,为我们提供方便快捷的 Spring MVC 开发环境。
public class SpringMvcConfig {
}
--controller包
//UserController
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'info':'springmvc'}";
}
//post带中文参数的接收
@RequestMapping("/getUser")
@ResponseBody
public String getUser(String name,int age){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
return "{'name':'"+name+"','age':'"+age+"',}";
}
//post发送json数据的接收
@RequestMapping("/getJson")
@ResponseBody
public String getJson(@RequestBody User user){
System.out.println("姓名:"+user.getName());
System.out.println("年龄:"+user.getAge());
return "{'name':'"+user.getName()+"','age':'"+user.getAge()+"',}";
}
}
--mapper包
public interface UserDao {
@Insert("insert into tbl_user(name,age)values(#{name},#{age})")
public void save(User user);
}
--pojo包
@Data
public class User {
private Integer id;
private String name;
private Integer age;
//setter..getter..toString略
}
--service包
public interface UserService {
public void save(User user);
}
--impl
@Service
public class UserServiceImpl implements UserService {
@Override
public void save(User user) {
System.out.println("user service ...");
}
}
2-请求参数
2.1-普通参数
①接口参数
请求类型--get
请求地址--http://localhost/commonParamDifferentName?name=张三&age=18
请求参数--name=张三&age=18
请求名称:普通参数请求
②接收代码
@RequestMapping("/commonParamDifferentName")
@ResponseBody
public String commonParamDifferentName(String userName , int age){
System.out.println("普通参数传递 userName ==> "+userName);
System.out.println("普通参数传递 age ==> "+age);
return "{'module':'common param different name'}";
}
2.2-pojo参数
①接口参数
请求类型--post
请求地址--http://localhost/pojoParam
请求参数--name=张三&age=18
请求名称:pojo参数请求
②接收代码
//POJO参数:请求参数与形参对象中的属性对应即可完成参数传递
@RequestMapping("/pojoParam")
@ResponseBody
public String pojoParam(User user){
System.out.println("pojo参数传递 user ==> "+user);
return "{'module':'pojo param'}";
}