1

Here is the situation.

When I use EclipseLink MOXy with Spring 3 MVC to generate JSON output it throws following exception when I try to set the json media type with following statement.

marshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");

javax.xml.bind.PropertyException: name: eclipselink.media-type value: application/json
    at org.eclipse.persistence.jaxb.JAXBMarshaller.setProperty(JAXBMarshaller.java:520)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)

I am using JAXBviews too to generate xml output for other services in the application. Here is the beans declaration in application-context.xml for the same.

<bean id="jaxbMarshaller"    class="com.abc.restws.marshallers.ClasspathScanningJaxb2Marshaller">
    <property name="basePackages" ref="jaxbBasePackages" />
</bean> 


    <bean id="jaxbMarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <property name="marshaller" ref="jaxbMarshaller"/>
    </bean> 
    <bean id="jaxbJsonMarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView">
        <property name="contentType" value="application/json"/>
        <property name="marshaller" ref="jaxbJsonMarshaller"/>
    </bean> 

What's the reason for this error? How can i fix this error? XML output is generated fine but for JSON it throws this exception. You can see in the exception details it's using correct JAXBMarshaller class too to set the json media type. Please advice.

"org.eclipse.persistence.jaxb.JAXBMarshaller.setProperty(JAXBMarshaller.java:520)"

4

1 回答 1

1

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

JSON 绑定在 2.4 版中添加到 EclipseLink,您使用的是包含 EclipseLink 2.1.2 的 WebLogic 10.3.4 (11g)。此问题的推荐解决方案是在 WebLogic 中为较新版本的 EclipseLink 创建一个共享库。

创建共享库

WebLogic 有共享库的概念。这些被部署为 EAR。下面是 EAR 在为 EclipseLink 2.4 创建共享库时的样子。

EclipseLink24.ear

  • 库/eclipselink.jar
  • META-INF/application.xml
  • 元信息/清单.MF
  • META-INF/weblogic-application.xml

应用程序.xml

<application>
  <display-name>EclipseLink 2.4 Shared Library</display-name>
  <module>
    <java></java>
  </module>
</application>

清单文件

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_04-b21 (Oracle Corporation)
Extension-Name: EclipseLink-2.4.0
Specification-Version: 2.4.0
Implementation-Version: 2.4.0.v20120608-r11652

weblogic-application.xml

<weblogic-application>
  <prefer-application-packages>
    <package-name>org.eclipse.persistence.*</package-name>
  </prefer-application-packages>
</weblogic-application>

使用共享库

部署共享库后,您需要配置企业应用程序以使用它。

示例应用程序.ear

  • 元信息/清单.MF
  • META-INF/weblogic-application.xml
  • SampleApplication.war

weblogic-application.xml

weblogic-application.xml文件用于引用共享库。元素中的条目library-ref需要与共享库中 MANIFEST.MF 中的相应条目相匹配。

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/javaee_5.xsd http://xmlns.oracle.com/weblogic/weblogic-application http://xmlns.oracle.com/weblogic/weblogic-application/1.2/weblogic-application.xsd">
    <!--weblogic-version:10.3.4-->
    <wls:application-param>
        <wls:param-name>webapp.encoding.default</wls:param-name>
        <wls:param-value>UTF-8</wls:param-value>
    </wls:application-param>
    <wls:library-ref>
        <wls:library-name>EclipseLink-2.4.0</wls:library-name>
        <wls:specification-version>2.4.0</wls:specification-version>
        <wls:implementation-version>2.4.0.v20120608-r11652</wls:implementation-version>
        <wls:exact-match>true</wls:exact-match>
    </wls:library-ref>
</wls:weblogic-application>

测试服务程序

下面是一个测试 servlet,您可以将其包含在 WAR 中以测试 EclipseLink 版本。

package com.example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public TestServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.append("<html><body>");
        out.append(org.eclipse.persistence.Version.getVersion());
        out.append("</body></html>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

了解更多信息

于 2012-07-17T17:59:08.093 回答