Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

Java RESTful API - JSON repsonse doesn't return some fields of an Object

I am a newbie when it comes to Java RESTful API's. My current problem is that the JSON response is not returning some fields. Here is what I mean;

I have some JSON code which will be used to create a user profile.

{
    "fName": "fUser",
    "sName": "sUser",
    "username": "helloUser"
}

I send this code to the appropriate POST method and afterwards I am expecting a JSON response. This is what I expect the response to be;

{
    "id": "003629d7-90ea-4139-9752-c9a8a21306f6",
    "fName": "fUser",
    "sName": "sUser",
    "username": "helloUser",
    "dateCreated: "Thu Jul 14 13:42:29 BST 2016"
}

Instead the JSON response that I get back doesn't have the id and dateCreated field. I was expecting them in the JSON response because I assign a value to these fields in the Profile constructor.

DatbaseClass.java

public class DatabaseClass {

private static Map<Long, Message> messages = new HashMap<>();
private static Map<String, Profile> profiles = new HashMap<>();

public static Map<Long, Message> getAllMessages() {
    return messages;
}

public static Map<String, Profile> getAllProfiles() {
    return profiles;
}
}

Profile.java

@XmlRootElement
public class Profile {

private String id;
private String username;
private String fName;
private String sName;
private Date dateCreated;

public Profile() {}

// Here I set the id and the dateCreated. Which is why I am expecting 
// the JSON response to show them.
public Profile(String username, String fName, String sName) {
    this.id = UUID.randomUUID().toString();
    this.username = username;
    this.fName = fName;
    this.sName = sName;
    this.dateCreated = new Date();
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getfName() {
    return fName;
}

public void setfName(String fName) {
    this.fName = fName;
}

public String getsName() {
    return sName;
}

public void setsName(String sName) {
    this.sName = sName;
}

public Date getDateCreated() {
    return dateCreated;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Profile [
id=");
    builder.append(id + "
");
    builder.append(", username=");
    builder.append(username + "
");
    builder.append(", fName=");
    builder.append(fName + "
");
    builder.append(", sName=");
    builder.append(sName + "
");
    builder.append(", dateCreated=");
    builder.append(dateCreated);
    builder.append("
]" + "

");
    return builder.toString();
}
}

ProfileService.java

public class ProfileService {

private Map<String, Profile> profiles = DatabaseClass.getAllProfiles();

public ProfileService() {}

public List<Profile> getProfiles() {
    return new ArrayList<>(profiles.values());
}

public Profile getProfile(String username) {
    if(!profiles.containsKey(username)) {
        throw new NotFoundException(
                "The profile " + username + " does not exist."); 
    }
    return profiles.get(username);
}

public Profile addProfile(Profile profile) {
    final String username = profile.getUsername();
    if(profiles.containsKey(username)) {
        throw new BadRequestException(
                "The profile " + username + " already exists.");
    }
    profiles.put(profile.getUsername(), profile);

    return profiles.get(profile.getUsername());
}

public Profile updateProfile(Profile profile) {
    if(profile.getUsername().isEmpty()) {
        return null;
    }
    profiles.put(profile.getUsername(), profile);

    return profiles.get(profile.getUsername());
}

public Profile deleteProfile(String username) {
    return profiles.remove(username);
}
}

ProfileResource.java

@Path("profiles")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class ProfileResource {

private ProfileService pService = new ProfileService();

@POST
public Profile addProfile(Profile profile) {
    return pService.addProfile(profile);
}

@GET
public List<Profile> getProfiles() {
    return pService.getProfiles();
}

@GET
@Path("{username}")
public Profile getProfile(@PathParam("username") String username) {
    return pService.getProfile(username);
}

@PUT
@Path("{username}")
public Profile updateProfile(@PathParam("username") String username, 
        Profile profile) {
    return pService.updateProfile(profile);
}

@DELETE
@Path("{username}")
public Profile deleteProfile(@PathParam("username") String username) {
    return pService.deleteProfile(username);
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like object mapping mechanism provided by rest will call the default constructor, which is public Profile() {}, and use setter to put the username, fName, sName, instead of calling public Profile(String username, String fName, String sName).

You should set the id and create date in the service.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...