简体   繁体   中英

Sitebricks JSON default serializer. And why does it return text/json instead of application/json

Hmm...I intended on asking only one question. But I've instead decided to kill two birds with one stone .

First Question : What does Sitebricks use as the default serializer/deserializer between Java POJOs and JSON? For example, let's say that this is my POJO:

public class MyObject {
    private String key;
    private int value;
    public MyObject (String k, int v) {
        this.key = k;
        this.value = v;
    }
}

And this is an example of a method in my Sitebricks servlet:

@Get
public Reply<?> listPools() {
    return Reply.with(new MyObject("Foo", 6)).as(Json.class);
}

I did not annotate MyObject with any Jackson annotations, nor did I use GSON anywhere in my code. Yet, much to my surprise, I get back this as my content body in the response:

{"value":6,"key":"Foo"}

So what I would like to know is what technology or framework or what-not that Sitebricks is using by default to serialize-deserialize JSON. Jackson? GSON? Witchcraft and wizardry?

Now the Second Question . I noticed in the header that the response Content-Type was text/json . This looked weird to me because, in my past experience, I've always dealt with application/json as the Content-Type for JSON. This Stackoverflow post confirms my belief. Any comments on this point?

Thanks in advance!

Sitebricks uses Jackson by default which will convert objects without any configuration using sensible defaults. If you want to override the behavior, bind an ObjectMapper using standard Jackson-prescribed config.

You should probably set application/json for most cases, correct. SB should set this, you're right, I'll make the change in trunk.

并且仍然可以通过以下方式随时更改它

 return Reply.with(...).as(Json.class).type("application/json; charset=utf-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM