我有一个使用 CXF 提供 SOAP 和 REST 网络服务的 Java 服务器应用程序。目前它使用 JAX-B 的引用实现进行 XML 编码/解码,但我已将其配置为用 Jackson 替换 Jettison 进行 JSON 编码/解码。我使用 Spring 进行 DI 和应用程序上下文配置。
REST Web 服务配置片段如下所示:
web.xml
<servlet>
<display-name>Myapp REST Services</display-name>
<servlet-name>MyappWebServices</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyappWebServices</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
applicationContext.xml
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<jaxrs:server id="myappCoreSvcRest" address="/rest">
<jaxrs:serviceBeans>
<ref bean="fooService" />
<ref bean="barService" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:server>
此配置有效,将根据 HTTP Accept header 返回 XML 或 JSON。我喜欢这个配置的原因是它基于 Spring,而且创建和使用备用 JSON 编码器非常容易。有关配置 CXF 的详细信息,请参见 here .
我的问题是,现在我要提供一个新的(附加的)REST Web 服务,我想为这个新的 Web 服务使用不同的 JAX-B XML 绑定(bind)。我知道 MOXy 可以做到这一点,但我无法弄清楚如何配置 CXF 端点,以便它将 MOXy 用于编码/解码(以及如何告诉 Moxy 我的自定义 XML 映射文件)。我还希望这个新的 Web 服务根据 Accept header 返回 XML 或 JSON。我还了解到 MOXy 2.4+ 也可以解决这个问题!
理想情况下,我可以在不影响其他现有 servlet 的情况下将 MOXy 用于这个新端点。
最佳答案
注意:我是 EclipseLink JAXB (MOXy) JAXB (JSR-222) 的领导和成员专家组。
我不知道 CXF 的确切配置,但下面我提供了一些将 MOXy 与 Spring 结合使用的链接。欢迎随时contact me ,我可以帮助您实现这一点:
My problem is that now I have a new (additional) REST web service to provide and I would like to use a different JAX-B XML binding for this new web service. I understand that MOXy can do this, but I am unable to figure out how to configure a CXF end point so that it will use MOXy for marshalling/unmarshalling (and furthermore how to tell Moxy about my custom XML mapping file).
当将 MOXy 与 JAX-RS 实现一起使用时,您可以使用 ContextResolver
从 MOXy 的外部映射文件引导:
package blog.bindingfile.jaxrs;
import java.io.*;
import java.util.*;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import blog.bindingfile.Customer;
@Provider
@Produces({"application/xml", "application/json"})
public class CustomerContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext jc;
public CustomerContextResolver() {
ClassLoader cl = Customer.class.getClassLoader();
InputStream bindings =
cl.getResourceAsStream("blog/bindingfile/binding.xml");
try {
Map<String, Object> props = new HashMap<String, Object>(1);
props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, bindings);
jc = JAXBContext.newInstance(new Class[] {Customer.class} , props);
} catch(JAXBException e) {
throw new RuntimeException(e);
} finally {
try {
bindings.close();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
public JAXBContext getContext(Class<?> clazz) {
if(Customer.class == clazz) {
return jc;
}
return null;
}
}
复杂的例子
有关将 MOXy 与 Spring 结合使用的更多信息
I also would like this new web service to return either XML or JSON depending on the Accept header. I also have read that MOXy 2.4+ can handle that too!
是的,JSON 绑定(bind)被添加到 EclipseLink 2.4。要在您的应用程序中利用它,只需创建一个 MessageBodyReader
和一个 MessageBodyWriter
即可:
package org.example;
import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import javax.xml.transform.stream.StreamSource;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MOXyJSONProvider implements
MessageBodyReader<Object>, MessageBodyWriter<Object>{
@Context
protected Providers providers;
public boolean isReadable(Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
return true;
}
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
try {
Class<?> domainClass = getDomainClass(genericType);
Unmarshaller u = getJAXBContext(domainClass, mediaType).createUnmarshaller();
u.setProperty("eclipselink.media-type", mediaType.toString());
u.setProperty("eclipselink.json.include-root", false);
return u.unmarshal(new StreamSource(entityStream), domainClass).getValue();
} catch(JAXBException jaxbException) {
throw new WebApplicationException(jaxbException);
}
}
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 {
Class<?> domainClass = getDomainClass(genericType);
Marshaller m = getJAXBContext(domainClass, mediaType).createMarshaller();
m.setProperty("eclipselink.media-type", mediaType.toString());
m.setProperty("eclipselink.json.include-root", false);
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;
}
private JAXBContext getJAXBContext(Class<?> type, MediaType mediaType)
throws JAXBException {
ContextResolver<JAXBContext> resolver
= providers.getContextResolver(JAXBContext.class, mediaType);
JAXBContext jaxbContext;
if(null == resolver || null == (jaxbContext = resolver.getContext(type))) {
return JAXBContext.newInstance(type);
} else {
return jaxbContext;
}
}
private Class<?> getDomainClass(Type genericType) {
if(genericType instanceof Class) {
return (Class<?>) genericType;
} else if(genericType instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) genericType).getActualTypeArguments()[0];
} else {
return null;
}
}
}
您还可以创建 JSONProvider
的扩展:
了解更多信息
关于java - 使用 Spring 配置 CXF 以使用 MOXY 进行 XML 编码/解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8980204/