@JsonIgnore忽略屬性,返回的json中不包含字段


@JsonIgnore的使用:

實體類中加@JsonIgnore注解

package com.baidu.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Transient;

import java.io.Serializable;

public class User implements Serializable{
    private static final long serialVersionUID = 8121761080892505330L;
    private String username;
    @Transient
    @JsonIgnore
    private transient String password;

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

controller層:

@RequestMapping("/select")
    @ResponseBody
    public BaseResponse select(@RequestBody UserParam userParam){
        //用戶重置
        userParam.setUsername("李雪雷2");
        userParam.setPassword("666662");
        try{
            List<User> users = userService.select();
            return BaseResponse.successCustom().setData(users).build();
        }catch (Exception e){
            e.printStackTrace();
            return BaseResponse.failedCustom("系統異常").build();
        }
    }

頁面代碼:

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/jquery-1.11.3.min.js"></script>
<title>首頁</title>
<script type="text/javascript">
function add(){
    var orders = [
        {
            orderNo : "H222255"
        },
        {
            orderNo : "H222256"
        }
    ]
    var user = {
        username : "劉亞超",
        password : "ab0715",
        orderList : orders
    }
    debugger;
    $.ajax({
        url: '/store/api/test/add1.json',
        type: "POST",
        data: JSON.stringify(user),//將對象序列化成JSON字符串
        dataType: "json",
        contentType : 'application/json;charset=utf-8', //設置請求頭信息
        async: false,
        success: function (result) {
            debugger;
        },
        error: function (xhr, ajaxOptions, thrownError) {
            debugger;
            alert("出錯了");
        }
    })
}

function selectaa(){
    var orders = [
        {
            orderNo : "H222255"
        },
        {
            orderNo : "H222256"
        }
    ]
    var user = {
        username : "劉亞超",
        password : "ab0715",
        orderList : orders
    }
    $.ajax({
        url: '/store/api/test/select.json',
        type: "POST",
        data: JSON.stringify(user),//將對象序列化成JSON字符串
        dataType: "json",
        contentType : 'application/json;charset=utf-8', //設置請求頭信息
        async: false,
        success: function (result) {
            debugger;
        },
        error: function (xhr, ajaxOptions, thrownError) {
            debugger;
            alert("出錯了");
        }
    })
}
</script>
</head>
<body>
<input type="button" onclick="add()" value="添加" name="sunmitBtn">
<input type="button" onclick="selectaa()" value="查詢" name="sunmitBtn">
</body>
</html>

返回結果:

后台:

前台:

前台接受到的json對象中已經忽略掉了password字段,只顯示username。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM