bennycode / wlc-webapp

Website for free programming tutorials and screencasts.
http://www.welovecoding.com/
6 stars 1 forks source link

REST v2 - Always name result sets #123

Open bennycode opened 10 years ago

bennycode commented 10 years ago

Verschiedene Java-JSON-Bibliotheken bieten an, bei einem REST-Call aus dem Result automatisch POJOs zu parsen. Damit das zuverlässig funktioniert, muss aber Folgendes gegeben sein: The JSON root must be a JSON object with a key. In unserem Fall ist die Response aber ein JSON Array und kein JSON Object.

URL:

Aktuelle Rückgabe

[
    {
        "id": 8,
        "created": 1403719826000,
        "lastModified": 1403719826000,
        "name": "Android",
        "slug": "android",
        "color": "#8CBE29",
        "playlists": [
            "http://www.welovecoding.com/rest/service/v2/categories/8/playlists/16",
            "http://www.welovecoding.com/rest/service/v2/categories/8/playlists/29",
            "http://www.welovecoding.com/rest/service/v2/categories/8/playlists/50"
        ],
        "numberOfVideos": 212,
        "languageCodes": [
            "DE",
            "EN"
        ]
    },
    {
        "id": 7,
        "created": 1403719826000,
        "lastModified": 1403719826000,
        "name": "C",
        "slug": "c",
        "color": "#EF9608",
        "playlists": [
            "http://www.welovecoding.com/rest/service/v2/categories/7/playlists/13",
            "http://www.welovecoding.com/rest/service/v2/categories/7/playlists/14"
        ],
        "numberOfVideos": 10,
        "languageCodes": [
            "DE"
        ]
    },
...

Um einheitliche Result-Sets zu bieten, sollte unser Result immer ein JSON Object ({ }) sein und Rückgabewerte benennen.

Beispiel

{
    "categories": [
        {
            "id": 8,
            "created": 1403719826000,
            "lastModified": 1403719826000,
            "name": "Android",
            "slug": "android",
            "color": "#8CBE29",
            "playlists": [
                "http://www.welovecoding.com/rest/service/v2/categories/8/playlists/16",
                "http://www.welovecoding.com/rest/service/v2/categories/8/playlists/29",
                "http://www.welovecoding.com/rest/service/v2/categories/8/playlists/50"
            ],
            "numberOfVideos": 212,
            "languageCodes": [
                "DE",
                "EN"
            ]
        },
        {
            "id": 7,
            "created": 1403719826000,
            "lastModified": 1403719826000,
            "name": "C",
            "slug": "c",
            "color": "#EF9608",
            "playlists": [
                "http://www.welovecoding.com/rest/service/v2/categories/7/playlists/13",
                "http://www.welovecoding.com/rest/service/v2/categories/7/playlists/14"
            ],
            "numberOfVideos": 10,
            "languageCodes": [
                "DE"
            ]
        }
    ]
}
bennycode commented 10 years ago

Approach works very well (check https://github.com/bennyn/wlc-fire-tv/commit/a150711a2cbdfbde54926a0674b4baf22652a347). But maybe we can find a solution without a mapping class (like CategoryResult.java). I am quite optimistic. :)

YserzJenkins commented 10 years ago

Consider using something like this

private static ObjectMapper createCombinedObjectMapper() {
        return new ObjectMapper()
                .configure(SerializationFeature.WRAP_ROOT_VALUE, true)
                .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
                .setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());
}

in https://github.com/bennyn/wlc-webapp/blob/master/src/main/java/com/welovecoding/tutorial/api/v2/ObjectMapperResolver.java which maybe is a better fit to resolve this from the framework.

bennycode commented 10 years ago

Yeah, maybe we should use that. I hoped that there is an annotation for it. But the createJaxbJacksonAnnotationIntrospector should allow us to use a Jackson annotation where we can define the name for the wrapped list. At the moment it is solved with a mapping class but I will keep my eyes open for another solution. Thanks, @YserzJenkins. Up till now I thought you were only a building tool. :grin: