1

为了解决另一个问题,我从使用 Jersey 转移到 EclipseLink MOXy 以从 JAXB 创建的对象模型(由 Sun JAXB 2.1.12 创建)生成 JSON。我注意到的一个区别是输出的格式

泽西输出

{"artist-list":{"offset":0,"count":1,"artist":[{"score":"100","type":"Group","id":"4302e264-1cf0-4d1f-aca7-2a6f89e34b36","name":"Farming Incident","sort-name":"Incident, Farming","gender":"male","country":"AF","disambiguation":"the real one","ipi-list":{"ipi":["1001","1002"]},"life-span":{"begin":"1999-04","ended":"true"},"tag-list":{"tag":[{"count":5,"name":"thrash"},{"count":11,"name":"güth"}]}}]}}

但 MOXy 给

"count" : "1",
   "offset" : "0",
   "artist" : [ {
      "id" : "4302e264-1cf0-4d1f-aca7-2a6f89e34b36",
      "type" : "Group",
      "score" : "100",
      "name" : "Farming Incident",
      "sort-name" : "Incident, Farming",
      "gender" : "male",
      "country" : "AF",
      "disambiguation" : "the real one",
      "ipis" : [ "1001", "1002" ],
      "life-span" : {
         "begin" : "1999-04",
         "ended" : "true"
      },
      "tags" : [ {
         "count" : "5",
         "name" : "thrash"
      }, {
         "count" : "11",
         "name" : "güth"
      } ]
   } ]
}

Moxy 更漂亮 :) 但是通过 Json 使我们的数据可用的原因之一是减少传输带宽,所以有可能让 MOXy 生成所有一条线,并且每条线周围没有额外的空间:?

4

1 回答 1

0

默认情况下,EclipseLink JAXB (MOXy)会将 JSON 编组为一行。要获得格式化输出,您需要在 上设置以下属性Marshaller

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

package forum11450509;

public class Root {

    private String foo;
    private int bar;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public int getBar() {
        return bar;
    }

    public void setBar(int bar) {
        this.bar = bar;
    }

}

jaxb.properties

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

以下代码演示了如何指定格式化输出:

package forum11450509;

import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        Root root = new Root();
        root.setFoo("ABC");
        root.setBar(123);

        System.out.println("One Line:");
        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(root, System.out);

        System.out.println("\nFormatted:");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

输出

下面是运行演示代码的输出:

One Line
{"bar":123,"foo":"ABC"}
Formatted:
{
   "bar" : 123,
   "foo" : "ABC"
}

JAX-RS

同样的行为也适用MOXyJsonProvider(参见: http ://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html )。

一条线

默认情况下,当您包含MOXyJsonProvider在 JAX-RS 应用程序中时,输出将被编组在一行中:

package org.example;

import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;

public class CustomerApplication  extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(2);
        set.add(MOXyJsonProvider.class);
        set.add(CustomerService.class);
        return set;
    }

}

格式化输出

您还可以配置MOXyJsonProvider生成格式化输出:

package org.example;

import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;

public class CustomerApplication  extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(1);
        set.add(ExampleService.class);
        return set;
    }

    @Override
    public Set<Object> getSingletons() {
        MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
        moxyJsonProvider.setFormattedOutput(true);
    }

} 
于 2012-07-12T13:13:13.637 回答