@JsonView注解

使用接口来声明多个视图

package org.byron4j.springMVC4_rest_jackson_jsonview_annotation;
 
public class Profile {
    public interface PublicView {}
    public interface FriendsView extends PublicView{}
    public interface FamilyView extends FriendsView {}
}

在值对象的get方法上指定视图

 
import org.byron4j.springMVC4_rest_jackson_jsonview_annotation.Profile;
 
import com.fasterxml.jackson.annotation.JsonView;
 
public class User {
    @JsonView(Profile.PublicView.class)     
    private String userId;
    private String password;    
    private int age;            
    @JsonView(Profile.FamilyView.class) 
    private long mobnum;        
    @JsonView(Profile.FriendsView.class)
    private String mailId;
    @JsonView(Profile.PublicView.class) 
    private String name;    
    @JsonView(Profile.PublicView.class)     
    private College college;
    @JsonView(Profile.PublicView.class)     
    private Address address;
    public User(String userId, String password, int age, long mobnum, String mailId, 
            String name, College college, Address address) {
        this.userId = userId;
        this.password = password;
        this.age = age;
        this.mobnum = mobnum;
        this.mailId = mailId;
        this.name = name;
        this.college = college;
        this.address = address;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public long getMobnum() {
        return mobnum;
    }
    public void setMobnum(long mobnum) {
        this.mobnum = mobnum;
    }
    public String getMailId() {
        return mailId;
    }
    public void setMailId(String mailId) {
        this.mailId = mailId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public College getCollege() {
        return college;
    }
    public void setCollege(College college) {
        this.college = college;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
}
在controller方法上指定视图


import java.util.List;
 
import org.byron4j.springMVC4_rest_jackson_jsonview_annotation.pojo.Name;
import org.byron4j.springMVC4_rest_jackson_jsonview_annotation.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
 
import com.fasterxml.jackson.annotation.JsonView;
@RestController
@RequestMapping("/app")
public class UserController {
    @Autowired
    private UserService userService;
    @JsonView(Profile.PublicView.class)
    @RequestMapping(value= "/publicprofile", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<user> getAllPublicProfile() {
        List<user> users = userService.getAllUsers();
        return users;
    }   
    @JsonView(Profile.FriendsView.class)
    @RequestMapping(value= "/friendsprofile", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<user> getAllFriendsProfile() {
        List<user> users = userService.getAllUsers();
        return users;
    }   
    @JsonView(Profile.FamilyView.class)
    @RequestMapping(value= "/familyprofile", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<user> getAllFamilyProfile() {
        List<user> users = userService.getAllUsers();
        return users;
    }       
    @RequestMapping(value= "/completeprofile/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public User getCompleteProfileById(@PathVariable(value = "userId") String userId) {
        User user = userService.getUserById(userId);
        return user;
    }
    @RequestMapping(value="/save", method = RequestMethod.POST)
    public ResponseEntity<string> save(@RequestBody Name name) {
        StringBuffer output = new StringBuffer();
        if (name.getFirstName() != null) {
            output.append(" ").append(name.getFirstName());
        }
        if (name.getMiddleName() != null) {
            output.append(" ").append(name.getMiddleName());
        }
        if (name.getLastName() != null) {
            output.append(" ").append(name.getLastName());
        }       
        return new ResponseEntity<string>(output.toString(), HttpStatus.CREATED);
    } 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javafanwk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值