springBoot maven 打包jar报错

发布于 2022-09-05 22:30:52 字数 12682 浏览 13 评论 0

代码可以正常运行,websocket也是正常工作,但打包成jar就会报如下错误

Error starting ApplicationContext. To display the auto-configuration             report re-run your application with 'debug' enabled.
2017-09-18 10:33:04,779:ERROR main (SpringApplication.java:839) -     Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating     bean with name 'serverEndpointExporter' defined in class path resource     [com/zenhome/cloud2cloud/service/gizwits_adapter/websocket/config/WebSocketCon    fig.class]: Invocation of init method failed; nested exception is     java.lang.IllegalStateException: javax.websocket.server.ServerContainer not     available
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:754) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111) [spring-boot-test-1.4.2.RELEASE.jar:1.4.2.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283) [surefire-junit4-2.18.1.jar:2.18.1]
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) [surefire-junit4-2.18.1.jar:2.18.1]
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) [surefire-junit4-2.18.1.jar:2.18.1]
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) [surefire-junit4-2.18.1.jar:2.18.1]
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) [surefire-booter-2.18.1.jar:2.18.1]
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) [surefire-booter-2.18.1.jar:2.18.1]
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [surefire-booter-2.18.1.jar:2.18.1]

Caused by: java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available

at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterPropertiesSet(ServerEndpointExporter.java:102) ~[spring-websocket-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
... 41 more

pom.xml里加的是

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>

websocketconfig.java

@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
    return new ServerEndpointExporter();
    }
}

MyWebsocket.java

package     com.zenhome.cloud2cloud.service.gizwits_adapter.websocket.controller;

import com.alibaba.fastjson.JSONObject;
import com.zenhome.cloud2cloud.service.gizwits_adapter.bean.DevEvent;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Administrator on 2017/7/26.n
 */
@ServerEndpoint(value="/websocket")
@Component
public class MyWebSocket {
//静态变量,用来记录当前在线连接数,应该把它设计成线程安全的
private static int onlineCount = 0;
private final int NUMBER = 10000;
private static ConcurrentHashMap<String,Session> mapUser = new ConcurrentHashMap<String, Session>();
private static ConcurrentHashMap<Session,String> userMap = new ConcurrentHashMap<Session, String>();

//concurrent包的线程安全set,用来存放每个客户端对应的MyWebSocket对象
private static CopyOnWriteArrayList<MyWebSocket> webSocketSet = new CopyOnWriteArrayList<MyWebSocket>();

//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
/**
 * 连接建立成功调用的方法
 */
    @OnOpen
    public void onOpen(Session session) throws IOException, InterruptedException
    {
    String mac;
    String mac_subkey;
    int subkey;
    subkey = (int)(Math.random()*NUMBER);

    this.session = session;
    mac = session.getQueryString().split("=")[1];
    System.out.println(mac = URLDecoder.decode(mac,"UTF-8"));
    mac_subkey = mac +"."+ subkey;
    System.out.println("mac_subkey = "+mac_subkey);
    session.getUserProperties().toString();
    //将用户名和连接的session存放在在线集合中
    userMap.put(session,mac_subkey);
    mapUser.put(mac_subkey,session);
    System.out.println("mapUser :"+mapUser);
    webSocketSet.add(this);//加入set中
    addOnlineCount();//在线数加1
    System.out.println("有新连接加入!当前在线人数为"+getOnlineCount());
    sendMessage("欢迎"+mac_subkey+"加入!");
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose(CloseReason reason)
    {
    String name = userMap.get(session);
    //在线列表移除关闭的用户名
    userMap.remove(session);
    mapUser.remove(name);

    webSocketSet.remove(this);//从set中删除
    subOnlineCount();//在线数减1
    System.out.println("有一连接关闭!当前在线人数为"+getOnlineCount());
    System.out.println("关闭一个websocket由于"+reason.getReasonPhrase());
}

    /**
     * 收到客户端消息后调用的方法
     * @param  message 客户端发送过来的消息
     */
    @OnMessage
    public void onMessage(String message, Session session) throws IOException, InterruptedException
    {
    System.out.println("来自客户端的消息:"+message);
    sendMessage(message);

    }

    /**
     * 实时消息转发
     * @param message
     * @throws IOException
     * @throws InterruptedException
     */
    public  void message(DevEvent message) throws IOException, InterruptedException
    {
    System.out.println("---------"+message.getMac());
    System.out.println("message.getData() AlarmZone  = "+message.getData().getString("AlarmZone"));
    System.out.println("message.getData() alarming  = "+message.getData().getString("alarming"));
    System.out.println("message.getData() Arm_Disarm  = "+message.getData().getString("Arm_Disarm"));

    JSONObject jsonObject = new JSONObject();
    if (message.getData().getString("AlarmZone").equals("1")){
        jsonObject.put("AlarmZone","警报!门锁有人闯入");
    }
    else if (message.getData().getString("AlarmZone").equals("2")){
        jsonObject.put("AlarmZone","警报!大厅有人闯入");
    }
    jsonObject.put("Arm_Disarm",message.getData().getString("Arm_Disarm"));
    jsonObject.put("mac",message.getMac());
    System.out.println("alarmInfo ="+jsonObject);

    for (String key : mapUser.keySet()){
        String[] device_mac = key.split("\\.");
        if(device_mac[0].equals(message.getMac())){
            mapUser.get(key).getBasicRemote().sendText(jsonObject.toString() + "\n");
        }

    }
    }

    /**
     * 发生错误时调用
     */
    @OnError
    public void onError(Session session, Throwable error)
    {
    System.out.println("发生错误");
    error.printStackTrace();
    }

    public void sendMessage(String message) throws             IOException,InterruptedException
    {
    this.session.getBasicRemote().sendText(message);

    }

    public static synchronized int getOnlineCount(){
    return onlineCount;
    }

    public static synchronized void addOnlineCount(){
    MyWebSocket.onlineCount++;
    }

    public static synchronized void subOnlineCount(){
    MyWebSocket.onlineCount--;
    }

    }

请问一下是什么原因导致打包失败?感谢感谢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

调妓 2022-09-12 22:30:52

出现这个问题主要是因为打包package遇到test会冲突(冲突解释不准确),在打包的时候跳过test就可以了
mvn package -DskipTests

梦中楼上月下 2022-09-12 22:30:52

我也遇到该问题,不知如何解决

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文