本文介绍了我如何使MOXy仅在一行上输出以节省空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To solve another problem I have moved from using Jersey to EclipseLink MOXy to generate JSON from a JAXB created object model ( created by Sun JAXB 2.1.12). One difference I've noticed is the formatting of the output

{"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生成全部一行,并且每行周围都没有多余的空格:?

Moxy is much prettier :) But one of the reasons to move to make our data available via Json is to reduce transmission bandwidth so is it possible to get MOXy to generate all one line, and without the extra spaces around each : ?

推荐答案

默认情况下 EclipseLink JAXB(MOXy) 会将JSON封送至一行.要获取格式化的输出,您需要在Marshaller上设置以下属性:

By default EclipseLink JAXB (MOXy) will marshal the JSON to one line. To get formatted output you need to set the following property on the 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

演示

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

The following code demonstrates how to specify formatted output:

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);
    }

}

输出

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

Below is the output from running the demo code:

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 ).

The same behaviour holds for MOXyJsonProvider (see: http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html).

一行

One Line

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

By default when you include MOXyJsonProvider in your JAX-RS application, the output will be marshalled on one line:

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;
    }

}

格式化输出

Formatted Output

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

You can also configure MOXyJsonProvider to produce formatted output:

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);
    }

} 

这篇关于我如何使MOXy仅在一行上输出以节省空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 08:23