edvin / tornadofx

Lightweight JavaFX Framework for Kotlin
Apache License 2.0
3.68k stars 272 forks source link

working with other JSON type format #1247

Open mostfa-ayad opened 4 years ago

mostfa-ayad commented 4 years ago

hello i try to start new project using tornadofx and springboot data rest spring boot data rest using a JSON format type application/hal+json like this

{ "_embedded" : { "items" : [ { "name" : "car", "_links" : { "self" : { "href" : "http://localhost:8080/items/1" }, "item" : { "href" : "http://localhost:8080/items/1" } } }, { "name" : "test", "_links" : { "self" : { "href" : "http://localhost:8080/items/2" }, "item" : { "href" : "http://localhost:8080/items/2" } } } ] }, "_links" : { "self" : { "href" : "http://localhost:8080/items" }, "profile" : { "href" : "http://localhost:8080/profile/items" } } } this format using two root _embedded and links and embedded has array of items that have my data my problem in read data insert work fine

the controller

class ItemController:Controller() {
    val api:Rest by inject()
    val path:String="items"
    var list=FXCollections.observableArrayList<Item>()
    fun loadItems():ObservableList<Item>{
        return api.get(path).list().toModel()
    }
    fun insertItem(item:Item){
        println(item.toJSON())
        api.post(path,item.toJSON()).ok()
    }
}
class Item:JsonModel {
    var id by property<Long>()
    var name by property<String>()

    fun idProperty()=getProperty(Item::id)
    fun nameProperty()=getProperty(Item::name)

    override fun toJSON (json:JsonBuilder){
        with(json){
            add("id",id)
            add("name",name)
        }
    }

    override fun updateModel(json: JsonObject) {
        with(json){
            id=long("id")
            name=string("name")
        }
    }
}
class ItemModel:ItemViewModel<Item>(){
    val id=bind {item?.idProperty()  }
    val name=bind { item?.nameProperty() }
}

i try this but can't get jsonArray api.get(path).consume().one().get("content")

i try to solve this problem with spring boot but i can't solve it and if i change the sprig boot JSON type to application/json this JSON is like this:

{ "links" : [ { "rel" : "self", "href" : "http://localhost:8080/items" }, { "rel" : "profile", "href" : "http://localhost:8080/profile/items" } ], "content" : [ { "value" : [ ], "rel" : null, "collectionValue" : true, "relTargetType" : "com.example.demo.Item" } ] }

SchweinchenFuntik commented 4 years ago

why there is no implementation code for item.toJson () ?

mostfa-ayad commented 4 years ago

@SchweinchenFuntik i edit the post now

SchweinchenFuntik commented 4 years ago

it turns out you get invalid JSON, you need to watch how the server sends data.

If you do not use Spring in principle, then you can use ktor + kotlinx serialization/

@ruckustboom have the opportunity to help?

mostfa-ayad commented 4 years ago

@SchweinchenFuntik
i usually using spring boot and write my own controllers and end points but i try to use spring boot data rest it very sample and make my live easy

Spring Data REST is built on top of the Spring Data project and makes it easy to build hypermedia-driven REST web services that connect to Spring Data repositories – all using HAL as the driving hypermedia type. It takes away a lot of the manual work usually associated with such tasks and makes implementing basic CRUD functionality for web applications quite simple.

i hope i can solve this with tornadofx

SchweinchenFuntik commented 4 years ago

I understand, but you need to make sure that the spring gives valid JSON. Based on the client code, I see no problems.

mostfa-ayad commented 4 years ago

@SchweinchenFuntik
i test it with RESTED Client and it work the problem is with tornadofx

SchweinchenFuntik commented 4 years ago

sorry again, does this come from a server or client?

{
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/items"
}, {
"rel" : "profile",
"href" : "http://localhost:8080/profile/items"
} ],
"content" : [ {
"value" : [ ],
"rel" : null,
"collectionValue" : true,
"relTargetType" : "com.example.demo.Item"
} ]
}
mostfa-ayad commented 4 years ago

from server image

SchweinchenFuntik commented 4 years ago

Well, this json does not match what the client is waiting for. On the client, do:

val items:  List<Item> = // create elements
println(items.toJSON())

full example:

class TestView : View() {
    override val root = vbox {
        val item = Item().apply { id = 0; name = "test"  }
        println(item.toJSON())
        println()
        println(listOf(item).toJSON())
    }
}

class Item : JsonModelAuto {
    var id by property<Int>()
    var name by property<String>()
}

console output:

{"id":0,"name":"test"}

[{"id":0,"name":"test"}]
mostfa-ayad commented 4 years ago

@SchweinchenFuntik thank you but the problem is with get the JSON data from the server

SchweinchenFuntik commented 4 years ago

this does not apply to tornadofx anymore, if you cannot change the server code, then it may be worth parsing manually.

also impossible to help without backend code

mostfa-ayad commented 4 years ago

@SchweinchenFuntik you are right i will change the server code thanks alot for your help

mustofa-id commented 4 years ago

Maybe, you should go with Traverson.