使用接口来声明多个视图
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);
}