package org.byron4j.springMVC4_rest_jackson_jsonview_annotation;
public class Profile {
public interface PublicView {}
public interface FriendsView extends PublicView{}
public interface FamilyView extends FriendsView {}
}
package org.byron4j.springMVC4_rest_jackson_jsonview_annotation;
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 getAllPublicProfile() {
List users = userService.getAllUsers();
return users;
}
@JsonView(Profile.FriendsView.class)
@RequestMapping(value= "/friendsprofile", produces = MediaType.APPLICATION_JSON_VALUE)
public List getAllFriendsProfile() {
List users = userService.getAllUsers();
return users;
}
@JsonView(Profile.FamilyView.class)
@RequestMapping(value= "/familyprofile", produces = MediaType.APPLICATION_JSON_VALUE)
public List getAllFamilyProfile() {
List 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 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(output.toString(), HttpStatus.CREATED);
}
}
UserService.java:
package org.byron4j.springMVC4_rest_jackson_jsonview_annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import org.byron4j.springMVC4_rest_jackson_jsonview_annotation.pojo.Address;
import org.byron4j.springMVC4_rest_jackson_jsonview_annotation.pojo.College;
import org.byron4j.springMVC4_rest_jackson_jsonview_annotation.pojo.User;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public User getUserById(String userId) {
Predicate userPredicate = u-> u.getUserId().equals(userId);
User obj = list.stream().filter(userPredicate).findFirst().get();
return obj;
}
public List getAllUsers() {
return list;
}
private List list = new ArrayList<>();
{
Address address = new Address("A-32", "Varanasi", "India");
College college = new College("UP College", "Varanasi");
User user = new User("mohan", "m123", 20, 988745 ,"mohan@gmail.com", "Mohan", college, address);
list.add(user);
address = new Address("C-50", "Allahabad", "India");
college = new College("Allahabad University", "Allahabad");
user = new User("krishna", "k123", 25, 988345, "krishna@gmail.com", "Krishna", college, address);
list.add(user);
address = new Address("D-40", "Delhi", "India");
college = new College("JNU", "Delhi");
user = new User("mahesh", "mh123", 30, 988765, "mahesh@gmail.com", "Mahesh", college, address);
list.add(user);
}
}