注意:启动方式为maven插件启动
以实体类方式接收json数据
注册配置类
package com.painter.config;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/916:05
*/
public class ServletInitializer extends AbstractDispatcherServletInitializer { // 注册配置类
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
// 注册我们的 springmvc config 配置类
annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
return annotationConfigWebApplicationContext;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
配置类
需要在类上添加@EnableWebMvc 注解
package com.painter.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @Author: Painter
* @project_name: SpringMVC
* @system_login: sunshine
* @time: 2022/10/916:00
*/
/**
* @Configuration 等同于创建以个xml配置文件
* @ComponentScan("com.painter.controller") 将com.painter.controller包下的所有类注入到ioc容器中
*
* 在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
*/
@Configuration
@ComponentScan("com.painter.controller")
@EnableWebMvc
public class SpringMVCConfig { // 配置类
}
控制类
package com.painter.controller;
import com.painter.entity.UserEntity;
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;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/110:00
*/
@Controller
@RequestMapping("/json")
public class MyController {
/**
* 以实体类接收接送数据
* http://localhost:8080/json/entityjson?userName=painter&password=123123
*
* @return
*/
@ResponseBody
@RequestMapping(value = "/entityjson",produces="text/html;charset=UTF-8")
public String entityjson(@RequestBody UserEntity userEntity){
return userEntity.toString();
}
}
实体类
package com.painter.entity;
/**
* @Author: Painter
* @project_name: spring_mvc
* @system_login: sunshine
* @time: 2022/10/110:02
*/
public class UserEntity {
private String userName;
private String password;
private IdEntity idEntity;
public IdEntity getIdEntity() {
return idEntity;
}
public void setIdEntity(IdEntity idEntity) {
this.idEntity = idEntity;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserEntity{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
", idEntity=" + idEntity +
'}';
}
}
public class IdEntity {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "IdEntity{" +
"id='" + id + '\'' +
'}';
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
maven.xml
添加依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.painter</groupId>
<artifactId>SpringMVC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringMVC Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- 整合springmvc框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- json对象转换-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.14</version>
</dependency>
<!-- 接受json数据依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<!-- 配置服务器插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<address>127.0.0.1</address>
<port>8080</port>
<path>/</path> <!-- 应用程序上下文-->
<ignorePackaging>true</ignorePackaging>
</configuration>
</plugin>
</plugins>
</build>
</project>
使用postman工具
访问地址 http://localhost:8080/json/entityjson
请求方法 post
请求参数 类型为json
{
"userName":"painter",
"password":"123456",
"idEntity":{
"id":"001"
}
}
响应内容
UserEntity{userName='painter', password='123456', idEntity=IdEntity{id='001'}}
以map集合接收json数据
在控制类中编辑代码
访问http://localhost:8080/json/mapjson
传递json参数
{
"userName":"painter",
"password":"123456",
"idEntity":{"id":"001"}
}
在接口的参数设置为 Map<String, Object> key为String类型 value为Object类型(可以是任何类型的数据)
/**
* 以map接收接送数据
* http://localhost:8080/json/mapjson
* 传递json数据 {"userName":"painter","password":"123456"}
* @return {userName=painter, password=123456}
*/
@ResponseBody
@RequestMapping(value = "/mapjson",produces="text/html;charset=UTF-8")
public String mapJson(@RequestBody Map<String, Object> map){
return map.toString();
}
以list集合接收json数据
在控制类中编辑代码
访问http://localhost:8080/json/listjson
传递json数组数据
[100,"101",{"id":"001"}]
/**
* 以list接收接送数据
* http://localhost:8080/json/listjson
*
* 传递json数据
* @return
*/
@ResponseBody
@RequestMapping(value = "/listjson",produces="text/html;charset=UTF-8")
public String listJson(@RequestBody List<Object> list){
return list.toString();
}