|
| 1 | +package samplest.jongo; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import org.bson.types.ObjectId; |
| 5 | +import org.jongo.marshall.jackson.oid.Id; |
| 6 | +import org.jongo.marshall.jackson.oid.MongoId; |
| 7 | +import org.jongo.marshall.jackson.oid.MongoObjectId; |
| 8 | +import restx.annotations.POST; |
| 9 | +import restx.annotations.RestxResource; |
| 10 | +import restx.factory.Component; |
| 11 | +import restx.jongo.JongoCollection; |
| 12 | +import restx.security.PermitAll; |
| 13 | + |
| 14 | +import javax.inject.Named; |
| 15 | + |
| 16 | +@RestxResource @Component |
| 17 | +public class MongoResource { |
| 18 | + public static class ObjectWithIdAnnotation { |
| 19 | + // @Id // 0.34 -> OK ; 0.35 -> KO! |
| 20 | + @MongoId @JsonProperty("_id") // 0.35 -> KO! |
| 21 | + private String id; |
| 22 | + private String label; |
| 23 | + |
| 24 | + public String getId() { return id; } |
| 25 | + public void setId(String id) { this.id = id; } |
| 26 | + public String getLabel() { return label; } |
| 27 | + public void setLabel(String label) { this.label = label; } |
| 28 | + } |
| 29 | + public static class ObjectWithObjectIdAnnotation { |
| 30 | + // @org.jongo.marshall.jackson.oid.ObjectId @Id // 0.34 -> OK ; 0.35 -> KO! |
| 31 | + @MongoId @MongoObjectId @JsonProperty("_id") // 0.35 -> KO! |
| 32 | + private String id; |
| 33 | + private String label; |
| 34 | + |
| 35 | + public String getId() { return id; } |
| 36 | + public void setId(String id) { this.id = id; } |
| 37 | + public String getLabel() { return label; } |
| 38 | + public void setLabel(String label) { this.label = label; } |
| 39 | + } |
| 40 | + public static class ObjectWithObjectIdType { |
| 41 | + // @Id // 0.34 -> OK ; 0.35 -> OK |
| 42 | + @MongoId @JsonProperty("_id") // 0.35 -> OK |
| 43 | + private ObjectId id; |
| 44 | + private String label; |
| 45 | + |
| 46 | + public String getId() { return id.toString(); } |
| 47 | + public void setId(ObjectId id) { this.id = id; } |
| 48 | + public String getLabel() { return label; } |
| 49 | + public void setLabel(String label) { this.label = label; } |
| 50 | + } |
| 51 | + |
| 52 | + private final JongoCollection objectWithIdAnnotationCollection; |
| 53 | + private final JongoCollection objectWithObjectIdAnnotationCollection; |
| 54 | + private final JongoCollection objectWithObjectIdTypeCollection; |
| 55 | + |
| 56 | + public MongoResource( |
| 57 | + @Named("objectWithIdAnnotationCollection") JongoCollection objectWithIdAnnotationCollection, |
| 58 | + @Named("objectWithObjectIdAnnotationCollection") JongoCollection objectWithObjectIdAnnotationCollection, |
| 59 | + @Named("objectWithObjectIdTypeCollection") JongoCollection objectWithObjectIdTypeCollection) { |
| 60 | + this.objectWithIdAnnotationCollection = objectWithIdAnnotationCollection; |
| 61 | + this.objectWithObjectIdAnnotationCollection = objectWithObjectIdAnnotationCollection; |
| 62 | + this.objectWithObjectIdTypeCollection = objectWithObjectIdTypeCollection; |
| 63 | + } |
| 64 | + |
| 65 | + @POST("/mongo/objectsWithIdAnnotation") |
| 66 | + @PermitAll |
| 67 | + public ObjectWithIdAnnotation createFoo(ObjectWithIdAnnotation foo) { |
| 68 | + this.objectWithIdAnnotationCollection.get().insert(foo); |
| 69 | + return foo; |
| 70 | + } |
| 71 | + |
| 72 | + @POST("/mongo/objectsWithObjectIdAnnotation") |
| 73 | + @PermitAll |
| 74 | + public ObjectWithObjectIdAnnotation createObjectWithObjectIdAnnotation(ObjectWithObjectIdAnnotation objectWithObjectIdAnnotation) { |
| 75 | + this.objectWithObjectIdAnnotationCollection.get().insert(objectWithObjectIdAnnotation); |
| 76 | + return objectWithObjectIdAnnotation; |
| 77 | + } |
| 78 | + |
| 79 | + @POST("/mongo/objectsWithObjectIdType") |
| 80 | + @PermitAll |
| 81 | + public ObjectWithObjectIdType createObjectWithObjectId(ObjectWithObjectIdType objectWithObjectIdType) { |
| 82 | + this.objectWithObjectIdTypeCollection.get().insert(objectWithObjectIdType); |
| 83 | + return objectWithObjectIdType; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments