JAXB / Jersey - 如何指定“schemaLocation” [英] JAXB/Jersey - How To Specify "schemaLocation"

查看:63
本文介绍了JAXB / Jersey - 如何指定“schemaLocation”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey创建一个宁静的Web服务编组XML。

I am using Jersey to create a restful web-service marshals XML.

我如何设置xsi:schemaLocation?

How would I set the xsi:schemaLocation?

回答展示如何直接在Marshaller上设置Marshaller.JAXB_SCHEMA_LOCATION。

This answer show how to set the Marshaller.JAXB_SCHEMA_LOCATION directly on the Marshaller.

我遇到的麻烦是Jersey将Java对象编组为XML。如何判断Jersey的架构位置是什么?

The trouble I am having is that Jersey is marshaling the Java objects into XML. How do I tell Jersey what the schema location is?

推荐答案

您可以创建 MessageBodyWriter 用于此用例。通过 ContextResolver 机制,您可以获得与您的域模型关联的 JAXBContext 。然后你可以从 JAXBContext 获得 Marshaller 并设置 JAXB_SCHEMA_LOCATION on and do the marshal。

You could create a MessageBodyWriter for this use case. Through the ContextResolver mechanism you can get the JAXBContext associated with your domain model. Then you can get a Marshaller from the JAXBContext and set the JAXB_SCHEMA_LOCATION on it and do the marshal.

package org.example;

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;

@Provider
@Produces(MediaType.APPLICATION_XML)
public class FormattingWriter implements MessageBodyWriter<Object>{

    @Context
    protected Providers providers;

    public boolean isWriteable(Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    public void writeTo(Object object, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap<String, Object> httpHeaders,
        OutputStream entityStream) throws IOException,
        WebApplicationException {
        try {
            ContextResolver<JAXBContext> resolver 
                = providers.getContextResolver(JAXBContext.class, mediaType);
            JAXBContext jaxbContext;
            if(null == resolver || null == (jaxbContext = resolver.getContext(type))) {
                jaxbContext = JAXBContext.newInstance(type);
            }
            Marshaller m = jaxbContext.createMarshaller();
            m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "foo bar");
            m.marshal(object, entityStream);
        } catch(JAXBException jaxbException) {
            throw new WebApplicationException(jaxbException);
        }
    }

    public long getSize(Object t, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType) {
        return -1;
    }

}






更新


另一个问题。我的rest资源和提供者之间的联系是什么?

One other question. What is the connection between the my rest resource and the provider?

您仍然以相同的方式实现资源。 MessageBodyWriter 机制只是一种覆盖XML写入方式的方法。 @Provider 注释是向JAX-RS应用程序发出的信号,要求此类自动注册。

You still implement your resource the same way. The MessageBodyWriter mechanism is just a way to override how the writing to XML will be done. The @Provider annotation is a signal to the JAX-RS application to have this class automatically registered.


我的资源类将返回 Foo 对象。我认为我应该实现
MessageBodyWriter< Foo>

My resource class would return a Foo object. I take it I should be implementing a MessageBodyWriter<Foo>?

如果您只希望将其应用于 Foo 类,则可以将其实现为 MessageBodyWriter< Foo> 。如果您希望它不仅仅适用于 Foo ,您可以实现 isWriteable 方法,以便为相应的方法返回true上课。

You could implement it as MessageBodyWriter<Foo> if you only want it applied to the Foo class. If you want it to apply to more than just Foo you can implement to the isWriteable method to return true for the appropriate classes.

这篇关于JAXB / Jersey - 如何指定“schemaLocation”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆