2016-07-14 4 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フィールドがありません。私はProfileコンストラクタでこれらのフィールドに値を割り当てているので、JSONレスポンスでそれらを期待していました。

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

私はあなたがそのコンストラクタをどこでも呼び出すことはありません。あなたはただ既存のオブジェクトを使用します。 また、次のようにメソッドを変更することもできます: 'publicプロファイルaddProfile(プロファイルプロファイル){ final String username = profile.getUsername(); if(profiles.containsKey(username)){ 新しいBadRequestExceptionをスローする( "プロファイル" + username + "は既に存在します。 } profile.setId(UUID.randomUUID()。toString()); プロファイル(setDateCreated(新しいDate()); profiles.put(profile.getUsername()、プロファイル); リターン特性; } ' – dty

+0

はゲッターとセッターが正しくフォーマットされていないことが考えられ、数を変更してみてください文字を大文字と小文字に変換する –

+0

ブレークポイントを使用してデバッグしましたか? – QuestionMarks

答えて

0

restによって提供されるオブジェクトマッピングメカニズムは、public Profile() {}という既定のコンストラクターを呼び出し、public Profile(String username, String fName, String sName)の代わりにユーザー名fName、sName、 を入れるためにsetterを使用します。

サービスでIDと作成日を設定する必要があります。

関連する問題