简体   繁体   中英

Java Controller and Service - Implement Getter and Setter in Service

I have the following controller and Service class,

UserController.java:

@RequestMapping(value = "/user/getUser")
@ResponseBody
public Map<String, Object> getUser(@ModelAttribute UserRequestModel userRequest, HttpServletRequest request)
        throws Exception {
    final IContext context = contextFactory.getContext(request);
    Map<String, Object> responseMap = new HashMap<>();

    String UserID = userService.getUserID(context, userRequest.getName());

    if (UserID != null) {
        UserDetails userDetails = userService.getUserDetails(context, userID);
        ...
    }

    ...

 return responseMap;
}

------
@RequestMapping(value = "/user/Booking")
@ResponseBody
public Map<String, Object> Booking(@RequestParam("bookingRequest") String RequestAsJSON,HttpServletRequest request) throws Exception {
    Map<String, Object> responseMap = new HashMap<>();

    final IContext context = contextFactory.getContext(request);
    UserList userList = userService.getUserList(context, RequestAsJSON);
    IUserResponse userresponse = userService.createBooking(RequestAsJSON, context, userList);
    return responseMap;
}

Also following Service class,

UserService.java :

@Override
public String getUserID(IContext requestContext, String userName) {
   String userID = null;
   List<User> users = createUser(context, userName);
   userID = <API>.getID(context, userList);
   return userID;
}

@Override
public UserDetails getUserDetails(IContext context, String userID) {
   UserDetails userDetails = null;
   userDetails = <API>.getUserDetails(userID, context);
   return userDetails;
}

@Override
public UserList getUserList(IContext context, String requestAsJSON) {
    JsonObject requestObj = new JsonParser().parse(requestAsJSON).getAsJsonObject();
    String userName = requestObj.get("Name").getAsString();
    UserCard userCard = createCard(userName);
    List<User> users = new ArrayList<>();
    users.add(userCard);
    UserList userList = new UserList();
    userList.setUsers(users);
    ..

    return userList;
}
    ....
private List<User> createUser(IContext context, String userName) {
   List<User> users = new ArrayList<>();
   UserCard userCard = createCard(userName);
   users.add(userCard);
   return users;
}

private UserCard createCard(String userName) {
   UserCard userCard = new UserCard();
   userCard.setOfficeID(12345);
   userCard.setAddress("Addr1");
   userCard.setOffice(“Test”);
   return userCard;
}

Currently createCard method is calling twice when getting user ("/user/getUser") and booking ("/user/Booking").

I would like to createCard while "/user/getUser" and get this card while "/user/Booking". I would like to get help in creating getter and setter.

In your service you might use a Map to store the name and userCard as key-value pairs:

static Map<String , UserCard> userCardInfo = new HashMap<>();

//inside createUser populate it

private List<User> createUser(IContext context, String userName) {
    List<User> users = new ArrayList<>();
    UserCard userCard = createCard(userName);
    users.add(userCard);
    this.userCardInfo.put(userName , userCard);
    return users;
}

//and inside getUserList retrieve from that map

public UserList getUserList(IContext context, String requestAsJSON) {
    JsonObject requestObj = new JsonParser().parse(requestAsJSON).getAsJsonObject();
    String userName = requestObj.get("Name").getAsString();
    UserCard userCard = null;
    if(this.userCardInfo.containsKey(userName)) {
        userCard = this.userCardInfo.get(userName);
    } else {
        userCard = createCard(userName);
    }

    List<User> users = new ArrayList<>();
    users.add(userCard);
    UserList userList = new UserList();
    userList.setUsers(users);
    return userList;
}

This two are separate request. REST is stateless so how can you do it ? Best Option can be You can pass data to client end that are you created on first request. And then for second Request Your client will serve this data with requestbody . You can put data on a static variable but its not a good practice.

You can do like this :

if (UserID != null) {
            UserDetails userDetails = userService.getUserDetails(context, userID);
            responseMap.put("userDetails",userDetails);
        }

And when call from client end then client should add usercard data to request . build a request that will accept usercardata on request object. May be your code already doing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM