Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:25:24+00:00 2026-06-06T06:25:24+00:00

This seems like another fairly simple thing to do, but I’m again struggling on

  • 0

This seems like another fairly simple thing to do, but I’m again struggling on how to do it.

I have a POJO, with Jersey/JAXB annotations that has HTTP POST and GET methods associated to it. When doing POST on the POJO, the request body is sent as a JSON, essentially modeling the POJO. When doing GET, I want to return the POJO, but with only a subset of the POJO properties.

I tried using @XmlTransient on the properties I don’t want for the GET, but then I cannot use those properties during the HTTP POST.

First, here is my POJO (User.java)

import javax.xml.bind.annotation.*
@XmlRootElement
public class User {
  private String userName;
  private String userEmail;
  private String userType;  // Do not return this property in GET
  private String userTmpPassword;  // Do not return this property in GET

  // User constructor
  public User(String userName,...) {
    this.userName = userName;
    //...etc...
  }

  // getters and setters with @XmlElement on each attribute 
  //...etc...
  @XmlElement(name="user_name")
  public String getUserName() {
    return userName;
  }
  public String setUserName() {
    return userName;
  }
  //...etc...
}

Here is my RESTful service class:

public class userService{
  @POST
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response getUser(User userInfoAsJSON) {
    User user = new User(userInfoAsJSON.getUserName(), ...);
    // pseudo-code for persisting User
    writeUserToDB(user);
    return Response.status(200);
  }

  @GET
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response getUser(String Id) {
    // Pseudo-code for retrieving user
    User user = retrieveUserFromDB(user);
    Response.status(200).entity(user);
  }
}

As expected, my JSON response for the HTTP GET returns all the properties of User, like this:

{
  "user_name": "John Doe",
  "user_email": "john_doe@johndoe.com",
  "user_type": "Admin",
  "user_tmp_password": "abc_xyz"
}

Whereas, I would like to only return a couple attributes in the JSON response:

{
  "user_name": "John Doe",
  "user_email": "john_doe@johndoe.com"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:25:26+00:00Added an answer on June 6, 2026 at 6:25 am

    You could always return a partially populated User object as by default null values are not marshalled. You may even be able to implement this in your retrieveUserFromDB method to avoid the copy step.

      @GET
      @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
      public Response getUser(String Id) {
        // Pseudo-code for retrieving user
        User userFromDB = retrieveUserFromDB(user);
        User user = new User();
        user.setUserName(userFromDB.getUserName());
        user.setUserEmail(userFromDB.getUserEmail());
        Response.status(200).entity(user);
      }
    

    For More Information

    • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seems like this should be simple, but powershell is winning another battle with me.
This seems like it would be fairly simple, but I've been unable to find
This seems like it should be an easy thing to do, but for the
This seems like it should be really simple, but I'm unable to figure this
This seems like a fairly simple issue, however I could not find any straight
This seems fairly trivial but I can't seem to work it out I have
This seems like it should be obvious but I can't figure it out. Suppose
This seems like a basic question, but I haven't found any clear answers. Essentially,
This seems like a basic question, but it's still bugging me. Why doesn't MyObject
This seems like it should be something very easy to do, but every time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.