栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springMVC中java对象转换成json格式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springMVC中java对象转换成json格式

文章目录


提示:以下是本篇文章正文内容,下面案例可供参考
pom坐标



    4.0.0

    org.example
    spring
    pom
    1.0-SNAPSHOT
    
        spring_ioc_anno
        spring_mvc
        springMVC-demo2
        springMVC-demo3
        springMVC-rest
        springMVC-demo4
    

    
        1.8
        1.8
    


    
        
        
            org.springframework
            spring-webmvc
            5.3.13
        

        
        
            ch.qos.logback
            logback-classic
            1.2.3
        

        
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        

        
        
            org.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        


        
        
            org.springframework
            spring-context
            5.3.11
        

        
            junit
            junit
            4.12
            test
        
        

        
            org.springframework
            spring-test
            5.3.13
        
        
        
        
            mysql
            mysql-connector-java
            8.0.24
        
        
        
        
            com.alibaba
            druid
            1.2.8
        
        
        
        
            org.springframework
            spring-context
            5.3.11
        
        

        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        

        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.3
        
        

        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.1
        
    


web.xml



    
    
    
    
        字符集过滤器
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            字符集编码
            encoding
            UTF-8
        
        
            forceEncoding
            true
        
    

    
    
        characterEncodingFilter
        /*
    


    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /*
    
    
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springMVC.xml
        
        1
    

    
        DispatcherServlet
        /
    

springMVC.xml




    
    

    
    
        
        
        

            
                
                    

                        
                        

                        
                        
                        
                        
                    
                
            
        
    

    
    
        
        
            
                text/html
                application/json
            
        
    

    
    
    

封装实体类对象

package com.vector.controller.pojo;

public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String sex;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                ", age=" + age +
                ", sex='" + sex + ''' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public User() {
    }

    public User(Integer id, String username, String password, Integer age, String sex) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
}

核心

HttpController控制层

package com.vector.controller;

import com.vector.controller.pojo.User;
import org.springframework.http.RequestEntity;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class HttpController {

    @RequestMapping("/testResponseUser")
    @ResponseBody
    public User testResponseUser(){
        return new User(1001,"admin","123456",21,"male");
    }
}

index.html内容




    
    首页


    

首页

通过servletAPI的response对象响应浏览器数据
通过servletAPI的responseBody对象响应浏览器数据
通过servletAPI的responseUser对象响应浏览器数据

效果


通过这个学习.我学会了java对象转换成能被浏览器解析的json数据格式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号