2017-10-05 4 views
-1

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?

+0

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

+0

http://idownvotedbecau.se/noexceptiondetails/ – Andreas

+0

@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 ...: - | –

Répondre

0

J'ai finalement trouvé la solution;

Il est nécessaire un constructeur vide dans toutes les classes FlightDTO, HotelDTO, CarDTO plus je doit réécrire la méthode toJson comme suit:

public JSONObject toJson() { 
    JSONObject obj = new JSONObject().put("id", this.id).put("travelerId", this.travelerId).put("status", this.status); 

    if (flights != null) { 
     JSONArray flightArray = new JSONArray(); 
     for (int i = 0; i < flights.size(); ++i) { 
     flightArray.put(flights.get(i).toJson()); 
     } 
     obj.put("flights", flightArray); 
    } 

    if (hotels != null) { 
     JSONArray hotelArray = new JSONArray(); 
     for (int i = 0; i < hotels.size(); ++i) { 
     hotelArray.put(hotels.get(i).toJson()); 
     } 
     obj.put("hotels", hotelArray); 
    } 

    if (cars != null) { 
     JSONArray carArray = new JSONArray(); 
     for (int i = 0; i < cars.size(); ++i) { 
     carArray.put(cars.get(i).toJson()); 
     } 
     obj.put("cars", carArray); 
    } 
    return obj; 
    } 

Et c'est le FlightDTO;

public class FlightDTO { 
    @MongoObjectId 
    private String id; 

    private String departure; 
    private String arrival; 
    private String airline; 
    private Double price; 

    public FlightDTO() { 
    } 

    public FlightDTO(JSONObject data) { 
    this.departure = data.getString("departure"); 
    this.arrival = data.getString("arrival"); 
    this.airline = data.getString("airline"); 
    this.price = data.getDouble("price"); 
    } 

    public JSONObject toJson() { 
    return new JSONObject() 
      .put("id", this.id) 
      .put("departure", this.departure) 
      .put("arrival", this.arrival) 
      .put("airline", this.airline) 
      .put("price", this.price); 
    } 
} 

maintenant ça marche bien! :)