application.yml或者bootstrap.yml
java@RunWith(SpringRunner.class)
@SpringBootTest
public class ClientTest {
}
application.ymlapplication-dev.ymlapplication-local.yml或者
bootstrap.ymlbootstrap-dev.ymlbootstrap-local.ymljava//指定local文件的配置
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"spring.profiles.active=local"})
public class ClientTest {
}
xml <dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.3</version> <!-- 使用最新版本 -->
</dependency>
目的是项目启动的时候自动去连接webSocket地址
javaimport com.lhw.WsRobClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import java.net.URI;
import java.net.URISyntaxException;
@Configuration
public class WebSocketConfig {
@Value("${websocket.url}")
private String url;
@Bean
public WsRobClient wsClient() throws URISyntaxException {
WsRobClient client=new WsRobClient(new URI(url));
client.connect();
return client;
}
}
WebSocketServer的目的是为了实时推送数据到客户端,才使用webSocket的双全工的方式
spring简单实现有两种方式
在Spring框架中配置WebSocket服务器时,您可以选择使用WebSocketConfigurer接口或者通过@ServerEndpoint注解配合ServerEndpointExporter bean来配置WebSocket端点。这两种方式各有优缺点,适用于不同的场景。
优点:
缺点:
学习曲线: 需要熟悉Spring WebSocket的配置和使用方式。
优点:
缺点:
然而,如果您的项目比较简单,或者您更熟悉Java EE WebSocket API的标准实现方式,并且不需要与Spring框架进行深度集成,那么使用@ServerEndpoint注解和ServerEndpointExporter也是一种可行的选择。
springBoot项目需要引入spring的webSocket依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
配置类
java
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}
前端获取不到header里面的文件名 后端代码
java
try (OutputStream outputStream = res.getOutputStream()) {
res.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode((String) fileList.get(0).get("fileName"), "utf-8"));
res.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// 将字节数组写入输出流
outputStream.write((byte[]) fileList.get(0).get("bytes"));
// 刷新输出流,确保所有数据都被发送出去
outputStream.flush();
} catch (IOException e) {
// 异常处理
throw new SystemException(500, "导出word文件失败!");
}
加上
java res.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
当我们给一个继承了父类的子类上使用@Data、@ToString、@EqualsAndHashCode 注解时,IDEA 会发出警告,大概内容如下:
Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add ‘(callSuper=false)’ to your type.
在项目根目录下创建lombok.config文件
configconfig.stopBubbling = true lombok.tostring.callsuper=CALL lombok.equalsandhashcode.callsuper=CALL lombok.accessors.chain=true