tomcat 没有找到类定义错误jakarta/json/bind/JsonbBuilder

0aydgbwb  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(184)

我在JakartaEE maven项目中尝试使用Jsonb / JsonbBuilder时遇到错误。
错误堆栈跟踪

java.lang.RuntimeException: jakarta/json/bind/JsonbBuilder
    at org.apache.tomcat.websocket.pojo.PojoMessageHandlerBase.handlePojoMethodException(PojoMessageHandlerBase.java:119)
    at org.apache.tomcat.websocket.pojo.PojoMessageHandlerWholeBase.onMessage(PojoMessageHandlerWholeBase.java:107)
    at org.apache.tomcat.websocket.WsFrameBase.sendMessageText(WsFrameBase.java:415)
    at org.apache.tomcat.websocket.server.WsFrameServer.sendMessageText(WsFrameServer.java:129)
    at org.apache.tomcat.websocket.WsFrameBase.processDataText(WsFrameBase.java:515)
    at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:301)
    at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:133)
    at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:85)
    at org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:183)
    at org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:162)
    at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:157)
    at org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:60)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:59)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1762)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NoClassDefFoundError: jakarta/json/bind/JsonbBuilder
    at com.example.websockets.WebChat.onMessage(WebChat.java:21)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:577)
    at org.apache.tomcat.websocket.pojo.PojoMessageHandlerWholeBase.onMessage(PojoMessageHandlerWholeBase.java:105)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: jakarta.json.bind.JsonbBuilder
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1449)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1257)
    ... 22 more

聚甲醛

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>websockets</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>websockets</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>18</maven.compiler.target>
        <maven.compiler.source>18</maven.compiler.source>
    </properties>

    <dependencies>

        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-api</artifactId>
            <version>9.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

引发错误的方法和类导入

@OnMessage
public void onMessage(Session session, String text) {

    try(Jsonb jsonb = JsonbBuilder.create()) {
        Message message = jsonb.fromJson(text, Message.class);
        System.out.println("Message received: " + message);

        if(message.getType().equals("join")){
            session.getUserProperties().put("name", message.getUserName());
            session.getUserProperties().put("active", true);
            Message info = new Message(message.getUserName(), "join", message.getUserName() + " has joined the chat");
            Message updateUserList = new Message(message.getUserName(), "users", getUserNames(session));

            sendAll(session , info);
            sendAll(session , updateUserList);
        } else if(message.getType().equals("chat")){
            sendAll(session, message);
        }
    }catch (Exception e){
        throw new RuntimeException(e);
    }
}

看起来您的帖子大部分是代码;请添加更多详细信息。

import jakarta.json.Json;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.websocket.OnClose;
import jakarta.websocket.OnError;
import jakarta.websocket.OnMessage;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;

import java.util.stream.Collectors;

当我尝试使用JsonbBuilder时显示错误,并且我在maven依赖项中确实有jakarta/json/bind/JsonbBuilder。

zqry0prt

zqry0prt1#

更改为TomEE修复了该错误。

5kgi1eie

5kgi1eie2#

Tomcat不包含Json绑定规范的实现。您可以包含一个实现,也可以切换到其他完全实现Jakarta规范的应用服务器。

<dependency>
  <groupId>org.eclipse</groupId>
  <artifactId>yasson</artifactId>
  <version>2.0.4</version>
</dependency>
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>jakarta.json</artifactId>
    <version>2.0.1</version>
</dependency>

相关问题