[springmvc]乱码问题解决以及JSON和java对象转换的几种方法

7.乱码问题

在web服务器配置中直接加上下面的过滤处理

<!--配置编码过滤器-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--设置编码格式-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <!--设置编码格式的生效范围-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

8.json

  • 前后端分离时代,前后端工程师约定的方便数据交换格式

JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

json转换为js对象

//将json转json
 var obj =JSON.parse(json);
 console.log(obj);

js对象转换为json字符串

 //将js转换为json
var json= JSON.stringify(people);
console.log(json);

<%--
  Created by IntelliJ IDEA.
  User: 塔塔
  Date: 2022/7/25
  Time: 20:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript">
        var people={
            name:"哔哩哔哩",
            age:100,
            address:"www.bilibili.com"
        };

        //将js转换为json
       var json= JSON.stringify(people);
       console.log(json);

       //将json转json
        var obj =JSON.parse(json);
        console.log(obj);
    </script>
</head>
<body>

</body>
</html>

jackson使用

导包

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.13.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>

Jackson转换java和json数据格式

package com.spring.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spring.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author panglili
 * @create 2022-07-25-20:36
 */
@Controller
public class JsonController {
    //produces = "application/json;charset=utf-8"
    //json提供的数据编码处理
    @ResponseBody
    @RequestMapping(value = "/json",produces = "application/json;charset=utf-8")
    public String json1() throws JsonProcessingException {
        //jsckson 提供的将一个json字符与java对象自动转换的类
        ObjectMapper mapper = new ObjectMapper();

        //创建对象
        User user=new User("小镇",19,"男");
        //交给mapper转换
        String s = mapper.writeValueAsString(user);

        return s;


    }
}

json处理乱码还有一种方式,直接配置在mvc中,不需要每个请求上面都去搞

<mvc:annotation-driven>
   <mvc:message-converters register-defaults="true">
       <bean class="org.springframework.http.converter.StringHttpMessageConverter">
           <constructor-arg value="UTF-8"/>
       </bean>
       <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
           <property name="objectMapper">
               <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                   <property name="failOnEmptyBeans" value="false"/>
               </bean>
           </property>
       </bean>
   </mvc:message-converters>
</mvc:annotation-driven>

fastjson的使用

导包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.72</version>
</dependency>

直接调用它提供的JSON对象

@RequestMapping("/j2")
@ResponseBody
public String json(){
    User user=new User("小镇",19,"男");
    String s = JSON.toJSONString(user);
    return s;
}
posted @ 2022-08-02 17:28  路漫漫qixiuyuanxi  阅读(278)  评论(0编辑  收藏  举报