2016-07-14 106 views
-1

当谈到Java RESTful API时,我是一个新手。我目前的问题是,JSON响应没有返回一些字段。这是我的意思;Java RESTful API - JSON repsonse不返回对象的某些字段

我有这将被用来建立用户档案一些JSON代码。

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

我这个代码发送到相应的POST方法,后来我期待的JSON响应。这是我期待的回应;

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

相反,JSON响应,我回去没有iddateCreated领域。我期待他们在JSON响应中,因为我在Profile构造函数中为这些字段赋值。

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 [\nid="); 
    builder.append(id + "\n"); 
    builder.append(", username="); 
    builder.append(username + "\n"); 
    builder.append(", fName="); 
    builder.append(fName + "\n"); 
    builder.append(", sName="); 
    builder.append(sName + "\n"); 
    builder.append(", dateCreated="); 
    builder.append(dateCreated); 
    builder.append("\n]" + "\n\n"); 
    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); 
} 
} 
+0

我看不到你在任何地方调用构造函数..你只是使用现有的对象。 还可以修改方法如下: '公共资料addProfile(配置文件轮廓){ 最终字符串username = profile.getUsername(); 如果(profiles.containsKey(用户名)){ 抛出新BadRequestException( “配置文件” +用户名+ “已经存在。”); } profile.setId(UUID.randomUUID()。toString()); 轮廓(setDateCreated(新的Date()); profiles.put(profile.getUsername(),配置文件); 回报状况; }' – dty

+0

可能是getter和setter的格式不正确,尝试改变几字母大小写。 –

+0

你有没有使用断点调试? – QuestionMarks

回答

0

貌似 而不是调用public Profile(String username, String fName, String sName)休息后提供的服务将调用默认的构造函数,这是public Profile() {},并使用二传手把用户名,FName参数,SNAME对象映射机制。

应设置ID和创建服务的日期。