【问题标题】:Liferay 7.2 with JAX-RS/JAXB and JDK 11 IssueLiferay 7.2 与 JAX-RS/JAXB 和 JDK 11 问题
【发布时间】:2019-08-06 19:57:18
【问题描述】:

我有created a Liferay Blade "rest" module 以及随该模板提供的示例代码。我正在尝试访问示例代码的 /greetings 端点:http://localhost:8080/o/greetings

我收到的错误似乎很常见,well-documented 因为在 Java 11 中,JAXB 已完全从 JDK 中删除:

发生了 JAXBException:在模块路径或类路径上找不到 JAXB-API 的实现。org.apache.aries.jax 找不到 com.sun.xml.internal.bind.v2.ContextFactory。 rs.whiteboard_1.0.4

该模块使用 gradle 构建并部署到 Liferay 7.2 中,没有错误,suggested solutions 没有解决错误(显然)。

完全披露,我不是 Java 开发人员,因此显然缺乏对 Java 的基本了解,这可能会导致问题。

我已尝试将建议的依赖项包含到我的 gradle.build 文件中:

compile('javax.xml.bind:jaxb-api:2.3.0')
compile('javax.activation:activation:1.1')
compile('org.glassfish.jaxb:jaxb-runtime:2.3.0')

我还尝试将上述依赖项的 jar 文件下载并部署到 Liferay 7.2 中。没有运气。

我的 gradle.build 文件:

dependencies {
    compileOnly group: "javax.ws.rs", name: "javax.ws.rs-api", version: "2.1"
    compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
    compileOnly group: "org.osgi", name: "org.osgi.service.jaxrs", version: "1.0.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "4.4.0"

}

示例类文件:

package some.random.super.long.folder.path;

import java.util.Collections;
import java.util.Set;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.util.PropsUtil;

import javax.net.ssl.HttpsURLConnection;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Application;
import javax.ws.rs.ServerErrorException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.jaxrs.whiteboard.JaxrsWhiteboardConstants;

/**
 * @author andrew
 */
@Component(
    property = {
        JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE + "=/greetings",
        JaxrsWhiteboardConstants.JAX_RS_NAME + "=Greetings.Rest"
    },
    service = Application.class
)
public class DiningRestServiceApplication extends Application {

    public Set<Object> getSingletons() {
        return Collections.<Object>singleton(this);
    }

    @GET
    @Produces("text/plain")
    public String working() {
        return "It works!";
    }

    @GET
    @Path("/morning")
    @Produces("text/plain")
    public String hello() {
        return "Good morning!";
    }

    @GET
    @Path("/morning/{name}")
    @Produces("text/plain")
    public String morning(
        @PathParam("name") String name,
        @QueryParam("drink") String drink) {

        String greeting = "Good Morning " + name;

        if (drink != null) {
            greeting += ". Would you like some " + drink + "?";
        }

        return greeting;
    }

}

我所追求的预期结果是在指定样本的端点接收样本消息。

非常感谢这个精彩社区的任何帮助!

更新

我在我的机器上切换到 Java 9 JDK。我仍然遇到上述错误消息。

【问题讨论】:

    标签: jaxb jax-rs java-11 liferay-7


    【解决方案1】:

    Liferay 社区的 Jorge Diaz 刚刚在 Liferay 论坛中回复说,这看起来像是一个现有的错误,目前正在处理中。

    https://issues.liferay.com/browse/LPS-92576 https://issues.liferay.com/browse/LPS-97968

    无论如何,我通过降级到 Java 8 确认 Liferay 与 Java 9 和 11 的组合不能很好地与 JAXB 配合使用,这确实有效。

    【讨论】:

      【解决方案2】:

      Liferay 在产品中提供了 JAXB 实现。我们只需要配置 JVM 来查找它,而不是尝试查找默认的旧的。

      只需设置属性javax.xml.bind.JAXBContextFactory=com.sun.xml.bind.v2.ContextFactory 就可以了。您应该能够在您的环境或启动 Liferay 的 JVM 的可执行文件中设置它。

      如果以后的 Liferay 版本应该默认设置。

      【讨论】:

        【解决方案3】:

        Carlos Sierra 提供的解决方案有效。添加

        -Djavax.xml.bind.JAXBContextFactory=com.sun.xml.bind.v2.ContextFactory 
        

        VM 参数列表指向 Liferay 提供的 ContextFactory 实现。

        环境:openjdk 版本“11.0.5”2019-10-15 与 Liferay DXP 7.2.10 GA1 SP 2 (dxp-2-7210)

        【讨论】:

          猜你喜欢
          • 2023-04-09
          • 1970-01-01
          • 2015-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-14
          • 1970-01-01
          • 2020-06-08
          相关资源
          最近更新 更多