spring mvc (七) 接收json数据

注意:启动方式为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





maven.xml

添加依赖

       
            com.fasterxml.jackson.core
            jackson-databind
            2.9.0
       




  4.0.0

  com.painter
  SpringMVC
  1.0-SNAPSHOT
  war

  SpringMVC Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11

    

    
    
      org.springframework
      spring-webmvc
      5.2.10.RELEASE
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    

    
    
      com.alibaba
      fastjson
      2.0.14
    


    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.9.0
    




  


  
  
    
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        
          
127.0.0.1
8080 / true

使用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  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 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 list){
        return list.toString();
    } 
  

你可能感兴趣的:(Java,SpringMvc,spring,mvc,json)