news 2026/4/18 7:03:36

SpringBoot项目集成ONLYOFFICE

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot项目集成ONLYOFFICE

ONLYOFFICE 文档8.2版本已发布:PDF 协作编辑、改进界面、性能优化、表格中的 RTL 支持等更新

文章目录
  • 前言
  • ONLYOFFICE 产品简介
  • 功能与特点
  • Spring Boot 项目中集成 OnlyOffice
    • 1. 环境准备
    • 2. 部署OnlyOffice Document Server
    • 3. 配置Spring Boot项目
    • 4. 实现文档编辑功能
    • 5. 文档保存与版本控制
      • (1)配置文件 `application.properties`
      • (2)依赖管理 `pom.xml`
      • (3)控制器 `DocumentController.java`
      • (4)配置类 `OnlyOfficeConfig.java`
      • (5)文档服务类 `DocumentService.java`
      • (6)文档模型类 `Document.java`
      • (7)前端页面 `index.html`
      • (8)用户服务类 `UserService.java`(可选)
      • (9)修改控制器以添加权限验证 `DocumentController.java`
    • 6. 安全性和权限管理
  • 体验与测评
  • 8.2 版本的其他更新:
  • 结尾:ONLYOFFICE 项目介绍

前言

提示:这里可以添加本文要记录的大概内容:

随着互联网技术的发展,越来越多的企业和个人开始寻求高效的在线文档处理解决方案。传统的本地文档编辑软件虽然功能强大,但在多用户协同工作方面存在诸多不便。为了满足这一需求,市场上涌现出了许多优秀的在线文档编辑工具,其中OnlyOffice因其出色的性能和灵活的集成能力而受到广泛好评。本文将详细介绍如何在Spring Boot项目中集成OnlyOffice,实现文档的在线编辑功能,并分享OnlyOffice的产品特点和用户体验。

ONLYOFFICE 产品简介

OnlyOffice是一套完整的开源办公套件,旨在为企业和个人提供高效的文档处理解决方案。它包含文字处理、电子表格和演示文稿三种类型的文档编辑器,支持多种文档格式的读取和编辑。OnlyOffice不仅提供了丰富的桌面应用程序,还拥有强大的Web版编辑器——OnlyOffice Document Server,后者可以方便地集成到各类Web应用中,实现文档的在线编辑和协作。

功能与特点

  • 全面的文档支持:支持DOCXXLSXPPTX等多种文档格式的编辑和转换。
  • 实时协作编辑:允许多个用户同时编辑同一文档,所有更改实时同步。
  • 权限管理:提供细粒度的文档访问权限控制,确保文档的安全性。
  • 版本管理:自动记录文档的历史版本,便于追踪修改记录和恢复早期版本。
  • 插件扩展:支持通过插件扩展编辑器的功能,满足更多个性化需求。
  • API接口丰富:提供RESTful APIJavaScript API,方便开发者集成到现有系统中。

Spring Boot 项目中集成 OnlyOffice

1. 环境准备

  • Java环境:确保已经安装了Java环境(建议JDK 1.8及以上版本)。

  • 构建工具:安装MavenGradle作为项目的构建工具。

  • Spring Boot项目:可以通过Spring Initializr快速创建一个Spring Boot项目基础框架。

2. 部署OnlyOffice Document Server

  • 安装OnlyOffice:可以选择在本地或云服务器上安装OnlyOffice Document Server。官方提供了详细的安装指南,可以根据自己的操作系统选择合适的安装方法。

  • 检查安装:安装完成后,确保Document Server能够正常访问,通常可以通过浏览器访问http://<your_server_ip>:80来检查是否成功安装。

3. 配置Spring Boot项目

  • 添加配置:在Spring Boot项目的application.properties文件中添加OnlyOffice服务器的相关配置信息,如服务器地址等。

    onlyoffice.server.url=http://<your_server_ip>
    onlyoffice.server.secret=<your_secret_key>

  • 创建控制器:创建一个控制器(Controller)用于处理前端请求,调用OnlyOffice API进行文档的加载、保存等操作。

    @RestController
    @RequestMapping(“/documents”)
    public class DocumentController {

    @Value("${onlyoffice.server.url}") private String serverUrl; @Value("${onlyoffice.server.secret}") private String secretKey; @GetMapping("/{id}") public ResponseEntity<?> getDocument(@PathVariable String id) { // 调用OnlyOffice API获取文档编辑所需参数 // ... } @PostMapping("/{id}") public ResponseEntity<?> saveDocument(@PathVariable String id, @RequestBody Document document) { // 处理文档保存逻辑 // ... }

    }

4. 实现文档编辑功能

  • 前端集成:利用OnlyOffice提供的JavaScript API,在前端页面中嵌入文档编辑器。

5. 文档保存与版本控制

  • 保存文档:当用户完成编辑并保存文档时,OnlyOffice会将更新后的文档发送回指定的回调URL。

    @PostMapping(“/{id}”)
    public ResponseEntity<?> saveDocument(@PathVariable String id, @RequestBody Document document) {
    // 将文档保存到数据库或文件系统中
    // 记录版本信息
    return ResponseEntity.ok().build();
    }

  • 版本管理:记录文档的历史版本,便于追踪修改记录和恢复早期版本。

以下是Spring Boot项目中集成OnlyOffice的完整代码部分:

(1)配置文件application.properties
onlyoffice.server.url=http://<your_server_ip> onlyoffice.server.secret=<your_secret_key>
(2)依赖管理pom.xml
<dependencies> <!-- Spring Boot Web Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Thymeleaf Starter (可选,用于模板引擎) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Spring Boot Security Starter (可选,用于安全控制) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- JSON处理库 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> </dependencies>
(3)控制器DocumentController.java
package com.example.onlyoffice.controller; import com.example.onlyoffice.config.OnlyOfficeConfig; import com.example.onlyoffice.model.Document; import com.example.onlyoffice.service.DocumentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/documents") public class DocumentController { @Autowired private OnlyOfficeConfig onlyOfficeConfig; @Autowired private DocumentService documentService; @GetMapping("/{id}") public ResponseEntity<Document> getDocument(@PathVariable String id) { Document document = documentService.getDocumentById(id); if (document == null) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok(document); } @PostMapping("/{id}") public ResponseEntity<Void> saveDocument(@PathVariable String id, @RequestBody Document document) { documentService.saveDocument(id, document); return ResponseEntity.ok().build(); } @GetMapping("/config/{id}") public ResponseEntity<Object> getConfig(@PathVariable String id) { return ResponseEntity.ok(onlyOfficeConfig.getConfig(id)); } }
(4)配置类OnlyOfficeConfig.java
package com.example.onlyoffice.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Component public class OnlyOfficeConfig { @Value("${onlyoffice.server.url}") private String serverUrl; @Value("${onlyoffice.server.secret}") private String secretKey; public Map<String, Object> getConfig(String documentId) { Map<String, Object> config = new HashMap<>(); config.put("document", Map.of( "fileType", "docx", "key", documentId, "title", "Sample Document", "url", serverUrl + "/documents/" + documentId )); config.put("documentType", "word"); config.put("editorConfig", Map.of( "callbackUrl", serverUrl + "/documents/" + documentId, "mode", "edit", "lang", "en" )); config.put("customization", Map.of( "actionBar", false, "chat", false )); return config; } }
(5)文档服务类DocumentService.java
package com.example.onlyoffice.service; import com.example.onlyoffice.model.Document; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.ArrayList; import java.util.Collections; @Service public class DocumentService { private final Map<String, Document> documents = new HashMap<>(); private final Map<String, List<Document>> history = new HashMap<>(); public Document getDocumentById(String id) { return documents.get(id); } public void saveDocument(String id, Document document) { Document currentDocument = documents.get(id); if (currentDocument != null) { history.computeIfAbsent(id, k -> new ArrayList<>()).add(currentDocument); } documents.put(id, document); } public List<Document> getHistory(String id) { return history.getOrDefault(id, Collections.emptyList()); } }
(6)文档模型类Document.java
package com.example.onlyoffice.model; import lombok.Data; @Data public class Document { private String content; // 其他字段... }
(7)前端页面index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>OnlyOffice Document Editor</title> </head> <body> <div id="document-editor" style="height: 600px;"></div> <script src="https://<your_server_ip>/web-apps/apps/api/documents/api.js"></script> <script> async function loadDocumentEditor() { const response = await fetch('/documents/config/<document_id>'); const config = await response.json(); new DocsAPI.DocEditor("document-editor", config); } window.onload = loadDocumentEditor; </script> </body> </html>
(8)用户服务类UserService.java(可选)
package com.example.onlyoffice.service; import org.springframework.stereotype.Service; @Service public class UserService { public boolean hasAccess(String username, String documentId) { // 这里实现具体的权限验证逻辑 // 示例:假设所有用户都有访问权限 return true; } }
(9)修改控制器以添加权限验证DocumentController.java
package com.example.onlyoffice.controller; import com.example.onlyoffice.config.OnlyOfficeConfig; import com.example.onlyoffice.model.Document; import com.example.onlyoffice.service.DocumentService; import com.example.onlyoffice.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/documents") public class DocumentController { @Autowired private OnlyOfficeConfig onlyOfficeConfig; @Autowired private DocumentService documentService; @Autowired private UserService userService; @GetMapping("/{id}") public ResponseEntity<Document> getDocument(@PathVariable String id, @AuthenticationPrincipal UserDetails userDetails) { if (!userService.hasAccess(userDetails.getUsername(), id)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } Document document = documentService.getDocumentById(id); if (document == null) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok(document); } @PostMapping("/{id}") public ResponseEntity<Void> saveDocument(@PathVariable String id, @RequestBody Document document, @AuthenticationPrincipal UserDetails userDetails) { if (!userService.hasAccess(userDetails.getUsername(), id)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } documentService.saveDocument(id, document); return ResponseEntity.ok().build(); } @GetMapping("/config/{id}") public ResponseEntity<Object> getConfig(@PathVariable String id, @AuthenticationPrincipal UserDetails userDetails) { if (!userService.hasAccess(userDetails.getUsername(), id)) { return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); } return ResponseEntity.ok(onlyOfficeConfig.getConfig(id)); } }

以上代码涵盖了从配置文件、依赖管理、控制器、配置类、服务类、模型类到前端页面的完整实现。希望这些代码能帮助你在Spring Boot项目中成功集成OnlyOffice。如果有任何问题或需要进一步的帮助,请随时提问。

6. 安全性和权限管理

  • 权限控制:通过OnlyOffice提供的API设置文档的访问权限,例如只读、编辑等。

  • 后端验证:在Spring Boot后端实现相应的权限验证逻辑,确保只有授权用户才能访问特定的文档。

体验与测评

在实际使用中,OnlyOffice的表现令人满意。其界面设计简洁直观,用户可以轻松上手;编辑器响应速度快,即使在网络条件不佳的情况下也能保持流畅的操作体验。OnlyOffice对中文文档的支持也相当不错,无论是字体显示还是排版布局都达到了较高的水平。

  • 更快的文件加载速度:为了加快编辑器的打开速度,我们优化了加载脚本。与之前的版本相比:

打开普通文件 – 最高提速21%
打开演示文稿 – 最高提速17%

Excel:

PPT:

PDF:

当然也有一些不足之处需要注意。部分高级功能需要购买商业许可证才能使用,这可能会增加企业的成本负担。另外与其他一些在线文档编辑工具相比,OnlyOffice的社区活跃度稍显不足,遇到问题时可能难以获得及时的帮助和支持。

8.2 版本的其他更新:

协作编辑 PDF 文件;

文本文档中的域代码;

从第三方来源插入文本;

预设阿拉伯语数字编号;

电子表格中的迭代计算;

电子表格编辑器中的丝滑滚动;

在幻灯片上绘图;

演示文稿中的随机切换效果;

所有语言的词典更新和拼写检查改进;

新的图表类型,如直方图、瀑布图、漏斗图等。

结尾:ONLYOFFICE 项目介绍

OnlyOffice不仅仅是一款文档编辑工具,它背后是一个充满活力的开源项目。该项目始于2009年,由Ascensio System SIA公司发起并维护。多年来,OnlyOffice不断发展壮大,形成了一个包括文档编辑器、邮件客户端、项目管理等多个子项目的生态系统。目前,OnlyOffice在全球范围内拥有数百万用户,得到了广泛的认可和好评。

ONLYOFFICE 官网

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 0:53:13

Godot卡牌游戏框架:从概念到实现的完整开发指南

Godot卡牌游戏框架&#xff1a;从概念到实现的完整开发指南 【免费下载链接】godot-card-game-framework A framework which comes with prepared scenes and classes to kickstart your card game, as well as a powerful scripting engine to use to provide full rules enfo…

作者头像 李华
网站建设 2026/4/16 16:02:06

B站视频下载神器:零基础也能轻松掌握的4K高清下载技巧

B站视频下载神器&#xff1a;零基础也能轻松掌握的4K高清下载技巧 【免费下载链接】bilibili-downloader B站视频下载&#xff0c;支持下载大会员清晰度4K&#xff0c;持续更新中 项目地址: https://gitcode.com/gh_mirrors/bil/bilibili-downloader 还在为无法保存B站精…

作者头像 李华
网站建设 2026/4/17 5:40:43

LRCGet:离线音乐库批量歌词下载终极指南

LRCGet&#xff1a;离线音乐库批量歌词下载终极指南 【免费下载链接】lrcget Utility for mass-downloading LRC synced lyrics for your offline music library. 项目地址: https://gitcode.com/gh_mirrors/lr/lrcget 你是否拥有大量离线音乐文件&#xff0c;却苦于找不…

作者头像 李华
网站建设 2026/4/17 0:38:11

EdgeRemover终极指南:一键安全彻底卸载Edge浏览器

EdgeRemover终极指南&#xff1a;一键安全彻底卸载Edge浏览器 【免费下载链接】EdgeRemover PowerShell script to remove Microsoft Edge in a non-forceful manner. 项目地址: https://gitcode.com/gh_mirrors/ed/EdgeRemover 还在为Windows系统自带的Microsoft Edge浏…

作者头像 李华
网站建设 2026/3/25 13:17:29

如何快速迁移艾尔登法环存档:EldenRingSaveCopier完整指南

如何快速迁移艾尔登法环存档&#xff1a;EldenRingSaveCopier完整指南 【免费下载链接】EldenRingSaveCopier 项目地址: https://gitcode.com/gh_mirrors/el/EldenRingSaveCopier 在艾尔登法环的广阔世界中&#xff0c;数百小时的游戏进度和精心培养的角色一旦丢失就是…

作者头像 李华
网站建设 2026/4/17 14:21:54

springcloud springboot nacos版本对应

组件版本关系 Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Version Dubbo Version Seata Version 2.2.6.RELEASE 1.8.1 1.4.2 4.4.0 2.7.8 1.3.0 2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE 1.8.0 1.4.1 4.4.0 2.7.8 1.3.0 2.2.3.…

作者头像 李华