Closed Assambra closed 1 year ago
You need to add @EzyArrayBinding
to Character
, you can take a look docs here: https://youngmonkeys.org/ezyfox-library/guides/ezyfox-binding-marshal-unmarshal
i've added
@Data
@EzyCollection
@EzyArrayBinding
public class Character {
@EzyId
Long id;
Long accountId;
String name;
String sex;
String race;
String model;
}
but now i get some error without sending response if i only try to read the List
List<Character> chars = characterService.getAllCharacters(user);
public List<Character> getAllCharacters(EzyUser user) {
Account account = accountRepo.findByField("username", user.getName());
return characterRepo.findListByField("accountId", account.getId());
}
Error: Caused by: com.tvd12.ezyfox.binding.exception.EzyReadValueException: can't read value: {accountId=2, race=Humanoid, sex=female, name=Assambra, model=äsldkfälskdfälskdj, _id=1} to: com.assambra.game.common.entity.Character
Error: Caused by: java.lang.ClassCastException: com.tvd12.ezyfox.entity.EzyHashMap cannot be cast to com.tvd12.ezyfox.entity.EzyArray
Hmm, because you're using this class for the both mongodb entity and response, you should not do that, you need to remove @EzyArrayBinding from the class, create a new response class and convert from entity to response
Thanks again and you can close the issue.
CharacterListModel:
@Getter
@Builder
@EzyArrayBinding
public class CharacterListModel {
Long id;
Long accountId;
String name;
String sex;
String race;
String model;
}
CharacterController:
@EzyDoHandle(Commands.CHARACTER_LIST)
public void characterList(EzyUser user)
{
logger.info("user {} request character list", user.getName());
responseFactory.newArrayResponse()
.command(Commands.CHARACTER_LIST)
.data(
convert(characterService.getAllCharacters(user))
)
.user(user)
.execute();
}
private CharacterListModel[] convert(List<Character> characters)
{
CharacterListModel[] chars = new CharacterListModel[characters.size()];
int i=0;
for (Character character : characters) {
CharacterListModel characterListModel = CharacterListModel.builder()
.id(character.getId())
.accountId(character.getAccountId())
.name(character.getName())
.sex(character.getSex())
.race(character.getRace())
.model(character.getModel())
.build();
chars[i] = characterListModel;
i++;
}
return chars;
}
I didnt find only one time a example how to use responseFactory.newArrayResponse(), but this example is nested with other functions so its to hard to figured out what i realy need to polish this values to send over the network.
I want to send a List with this values:
What extra files i need (response, model, ModelToResponseConverter)and how have they to look, i tryed a lot but allways same error: Caused by: java.lang.IllegalArgumentException: has no writer for class com.assambra.game.common.entity.Character
Next question on the client side i use the following to reseive the data is this correct?