J'ai une classe qui contient d'autres propriétés d'autres classes et lorsque j'essaie de convertir json dans ma classe, une erreur s'affiche.Le mappage de JSON à la classe ne fonctionne pas
C'est ma classe:
import org.jongo.marshall.jackson.oid.MongoObjectId;
import org.json.JSONObject;
import java.util.List;
public class BusinessTravelDTO {
@MongoObjectId
private String id;
private String travelerId;
private BusinessTravelStatus status;
List<FlightDTO> flights;
List<HotelDTO> hotels;
List<CarDTO> cars;
public BusinessTravelDTO() { }
public BusinessTravelDTO(JSONObject data) {
this.travelerId = data.getString("travelerId");
this.status = BusinessTravelStatus.valueOf(data.getString("status"));
this.flights = HandlerUtil.getInputFlights(data.getJSONArray("flights"));
this.hotels = HandlerUtil.getInputHotels(data.getJSONArray("hotels"));
this.cars = HandlerUtil.getInputCars(data.getJSONArray("cars"));
}
public JSONObject toJson() {
return new JSONObject()
.put("id", this.id)
.put("travelerId", this.travelerId)
.put("status", this.status)
.put("flights", this.flights)
.put("hotels", this.hotels)
.put("cars", this.cars);
}
Et voici où je tente de convertir en classe:
public static JSONObject acceptBusinessTravel(JSONObject input) {
String btId = getStringField(input, "id");
MongoCollection businessTravels = getBTCollection();
// Here is the problem...
BusinessTravelDTO bt = businessTravels.findOne(new ObjectId(btId)).as(BusinessTravelDTO.class);
bt.setStatus(BusinessTravelStatus.Accepted);
businessTravels.save(bt);
return new JSONObject().put("message", "The business travel has been ACCEPTED by your manager. Check your email.");
}
Voici l'erreur que je reçois:
"error": "org.jongo.marshall.MarshallingException: Unable to unmarshall result to class path.data.BusinessTravelDTO from content { \"_id\" : { \"$oid\" : \"59d6905411d58632fd5bd8a5\"} , \"travelerId\"
En jongo docs est spécifié que la classe doit avoir un constructeur vide ... http://jongo.org/#mapping J'ai 2 constructeurs, j'ai essayé aussi avec @JsonCreator, mais pas de succès ... :(
Avez-vous une idée pourquoi il ne convertit pas? Pourrait-il être quelque chose lié à des champs à l'intérieur de BusinesTravelDTO comme List CarDTO par ex?
Le texte JSON semble avoir un champ '_id' d'un type d'objet avec un champ' $ oid', alors que votre 'BusinessTravelDTO' a un champ' id' de type 'String'. Comment vous attendiez-vous à cela? --- Je suis un peu confus cependant, puisque je ne peux pas voir où dans le code vous faites le marshalling. À quoi bon nous montrer la méthode 'acceptBusinessTravel()'? – Andreas
http://idownvotedbecau.se/noexceptiondetails/ – Andreas
@Andreas peut-être que vous avez manqué @ MongoObjectId, cette anotation vous permet d'utiliser mongo object id en classe, mais si vous ne le mettez pas, le '_id' est de toute façon créé derrière le scènes ...: - | –