I have some experience using Jersey < 2.0. Now I am trying to build a war application to provide a JSON Webservice API.

我现在花了相当长的时间来try 配置Moxy,它看起来比添加的内容要复杂得多

    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>

返回到Jersey&lt;2.0中的web.xml.

Is there some possibility to just say "please add json support"?

Currently I just get a lot of Internal Server Error errors without any log entries on the server and just think "I have to do something totally wrong, this can't be so hard"

谁能给我一个提示吗?

推荐答案

You can configure EclipseLink MOXy as the JSON-binding provider by configuring the MOXyJsonProvider class through a JAX-RS Application class.

Example #1

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

}

Example #2

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.setAttributePrefix("@");
        moxyJsonProvider.setFormattedOutput(true);
        moxyJsonProvider.setIncludeRoot(true);
        moxyJsonProvider.setMarshalEmptyCollections(false);
        moxyJsonProvider.setValueWrapper("$");

        Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
        namespacePrefixMapper.put("http://www.example.org/customer", "cust");
        moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
        moxyJsonProvider.setNamespaceSeparator(':');

        HashSet<Object> set = new HashSet<Object>(1);
        set.add(moxyJsonProvider);
        return set;
    }

} 

For More Information

Json相关问答推荐

Vega-Lite时钟(使用Vega-Lite中的计时器)

在ConvertFrom-Json之后需要从PowerShell对象中获取数据

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

如何用JQ打印JSON文件的路径和键值

使用 JSON 和相对日期设置日历视图中 SharePoint 列表项的背景 colored颜色 格式

Jolt规范:将嵌套数组中的null元素移除

Delphi 11.3无法从变体创建/添加JSON

从 oracle 数据库中的 json blob 打印值

如何迭代、动态加载我的表单输入元素,然后在 React 中的表单提交上检索输入值?

使用 jq Select 键:值并输出为数组

如何比较 JSON 文档并返回与 Jackson 或 Gson 的差异?

如何使用 Serde 使用顶级数组反序列化 JSON?

如何使用 json.net 将数据表转换为 json 字符串?

如何在 Perl 中将简单的哈希转换为 json?

Peewee 模型转 JSON

NSManagedObject 属性值的 NSNull 处理

JSON 格式的 Amazon S3 响应?

js 中奇怪的 JSON 解析行为,Unexpected token :

将 Pandas 数据框转换为嵌套 JSON

无法解析 JSON 文件中的 TAB