- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个用户实体以及数据/文件(pdf格式)。上传并保存到数据库很好,但是当我让用户进入 postman 时尝试发送获取请求方法,然后在数据字段中显示一些糟糕的数据,而且我无法在数据库中看到我的 pdf 文件。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
@Entity(name = "employee")
公共(public)类员工{
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false, length = 30)
private String name;
@Column(nullable = false, length = 30)
private String university;
@Column(nullable = false, length = 30)
private String department;
@Column(nullable = false, length = 2)
private Integer year_of_experience;
@Lob
private byte[] data;
2. Controller 类
@GetMapping
public List<Employee> getAllEmployee(){
return employeeService.getAllEmployee();
}
@GetMapping("{id}")
public Employee getEmployeeById(@PathVariable Integer id){
return employeeService.getEmployeeById(id);
}
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public void addEmployee(@RequestParam("file") MultipartFile file, @RequestParam("emp") String emp ) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Employee employee = objectMapper.readValue(emp,Employee.class);
employeeService.addEmployee(employee,file);
}
@Override
public void addEmployee(Employee employee, MultipartFile file) throws IOException {
for (int i = 0; i < employee.getExperienceList().size(); i++) {
Experience experience = employee.getExperienceList().get(i);
experienceService.addExperience(experience);
}
byte[] temp = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(temp);
employee.setData(temp);
employeeRepostitory.save(employee);
}
在标题中设置“form-data”的帖子方法,并选择 key 作为文件,下一个 key 是我的用户实体 key 作为文本。我也尝试使用内容类型(applicaiton/json)但不起作用。我该如何将这些意外数据转换为 pdf 文件或其他搁浅格式以供查看。
"id": 62,
"name": "raj",
"university": "ewu",
"department": "bba",
"year_of_experience": 3,
"data": "JVBERi0xLjcNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1VUykgL1N0cnVjdFRyZWVSb290IDkgMCBSL01hcmtJbmZvPDwvTWFya2VkIHRydWU+Pi9NZXRhZGF0YSAyNSAwIFIvVmlld2VyUHJlZmVyZW5jZXMgMjYgMCBSPj4NCmVuZG9iag0KMiAwIG9iag0KPDwvVHlwZS9QYWdlcy9Db3VudCAxL0tpZHNbIDMgMCBSXSA+Pg0KZW5kb2JqDQozIDAgb2JqDQo8PC9UeXBlL1BhZ2UvUGFyZW50IDIgMCBSL1Jlc291cmNlczw8L0ZvbnQ8PC9GMSA1IDAgUj4+L0V4dEdTdGF0ZTw8L0dTNyA3IDAgUi9HUzggOCAwIFI+Pi9Qcm9jU2V0Wy9QREYvVGV4dC9JbWFnZUIvSW1hZ2VDL0ltYWdlSV0gPj4vTWVkaWFCb3hbIDAgMCA2MTIgNzkyXSAvQ29udGVudHMgNCAwIFIvR3JvdXA8PC9UeXBlL0dyb3VwL1MvVHJhbnNwYXJlbmN5L0NTL0RldmljZVJHQj4+L1RhYnMvUy9TdHJ1Y3RQYXJlbnRzI
最佳答案
您应该看看社区项目 Spring Content 。该项目为您提供了一种类似于 Spring Data 的内容方法。它适用于非结构化数据(文档、图像、视频等),就像 Spring Data 适用于结构化数据一样。
您可以将其添加到您的项目中,如下所示:-
pom.xml
<!-- Java API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-jpa-boot-starter</artifactId>
<version>1.0.0.M9</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>1.0.0.M9</version>
</dependency>
Configuration
@Configuration
@EnableJpaStores
// enable REST API
@Import("org.springframework.content.rest.config.RestConfiguration.class")
public class ContentConfig {
}
NB: technically this configuration is not needed when using the Spring Boot starters, but included for clarity
要关联内容,请将 Spring Content 注释添加到您的帐户实体。
Enployee.java
@Entity
public class Employee {
// replace @Lob field with:
@ContentId
private String contentId;
@ContentLength
private long contentLength = 0L;
@MimeType
private String mimeType = "application/pdf";
创建“商店”:
EmployeeContentStore.java
@StoreRestResource
public interface EmployeeContentStore extends ContentStore<Employee, String> {
}
这就是创建 REST 端点以处理 URI /employees
下的员工内容所需的全部内容。当您的应用程序启动时,Spring Content 将查看您的依赖项(请参阅 Spring Content JPA 和 REST),查看您的 UserContentStore 接口(interface)并为 JPA 注入(inject)该接口(interface)的实现。它还将注入(inject)一个将 http 请求转发到该实现的 @Controller
。这使您不必自己实现任何这些。因此,在编程模型和操作上与 Spring Data 非常相似。
然后...
curl -X POST/employees/{employeeId} -H "Content-Type: application/pdf"-F "file=@/path/to/local/file.pdf"
会将/path/to/local/file.pdf
的内容存储到数据库中,并将其与id为employeeId
的员工实体关联起来。
curl/employees/{employeeId} -H“接受:application/pdf”
将再次获取它等等...支持完整的 CRUD。
由于您将内容与您的员工实体相关联,因此当您从 Spring Data 端点返回 json 响应时,它们将提供与内容相关的元数据,而不是您当前看到的“糟糕数据”方法。
HTH
关于java - 在数据库和 postman 上无法看到实际上传的文件(.pdf 格式)格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61079999/
我在 postman 的预请求脚本中有以下代码——它给了我当前日期。我宁愿想要昨天的日期(current_timestamp - 1 天)。 var current_timestamp = new D
Postman Pro 可以向多个 Free Postman 用户共享无限制的请求吗? 我正在考虑设置一个 Pro 帐户作为中央请求收集存储,然后 Free Postman 用户只需克隆/读取它即可。
有可能在 postman 内部使用数组变量吗? 例如在请求正文中: { "myData" : {{arrayVariable}} } 在数据文件中: { "arrayVariable"
我有一个 postman 集合,我想将一组三个 API 调用链接在一起,并使用 runner 函数提供数据文件。可以说他们是:/prepareUpload/upload/confirmUpload并且
如何从另一个 GET API 的预请求脚本选项卡调用 POST API 请求(登录 API 具有带用户名和密码字段的请求正文),该 API 在其请求 url 中使用来自上述 API 正文的 token
当前,可以从global和environment范围以及预请求脚本中的通用variable设置和获取变量。但是,文档尚不清楚,是否可以通过编程方式设置collection作用域变量。 例如 pm.en
我正在使用 postman (v7.7.3),当我上传文件以将正文作为表单数据发送时,我收到了以下警告。 “此文件不在您的工作目录中。与您共享此请求的队友将无法使用此文件。为了使协作更容易,您可以在“
standalone Postman client在哪里?对于 Windows,离线工作时保存集合吗? 为了澄清,我想找到禁用在线同步时 Postman 默认保存集合文件的位置。我不试图 export
我之前使用过 Postman Chrome 扩展以及 Postman Interceptor Extension在 Postman 中捕获 Chrome 发出的 HTTP 请求。 但是,现在 Post
在 Postman 中单击保存的请求后,我得到的 View 如下所示: 要查看正文,我需要单击“正文”。 这不是什么大问题,除非我要处理 20 个左右的请求,并在每个请求的正文中查找特定的内容,如果能
我正在查看 Postman 快捷方式面板,但没有看到在请求正文中注释部分代码的快捷方式,复制和粘贴非常烦人 发表评论。 最佳答案 根据快捷方式面板Ctrl +/打开快捷方式面板但我不小心在请求正文中这
是否可以在我的 postman 测试用例中访问响应大小的值? 虽然我能够看到响应大小以及状态代码和响应时间(我可以在使用responseCode.code和responseTime的测试用例中使用这两
我正在尝试使用 Windows native Postman 应用程序 (V4.8.0) 将简单的 Get 命令发布到我在本地主机上运行的服务,但它们失败了。 postman 控制台显示 Error:
Newman 帮助指定集合、环境和全局变量可以作为路径或 URL 传递。我可以看到如何从 Postman 获取收藏 URL(通过转到“共享”>“收藏链接”)。 如何在 Postman 中获取 Envi
我们正在扩展到一个大型微服务构建,并通过 postman 完成更多测试(现场验证、错误测试等)。好奇...您的团队如何组织大量 API 的集合? (按 API、按测试类型、按发布等)从一个团队传递到另
Postman 对我来说是一个了不起的工具,但我对使用变量有一些疑问。在我的收藏中,我有 4 个选项卡/请求。 首先是获取用于其他三个的 token ( token 在 15 分钟后过期,因此我必须经
由于所有 Postman 集合基本上都是 .json 文件,因此很难通过代码审查工具审查在 Postman 中编写的代码。 目前我继续在GitHub上查看这样的.json文件,但是不太方便。 例如,这
我正在尝试使用 Newman 自动化 postman 测试.授权有问题。授权 bearer token 发生变化并且是动态的。有谁知道如何自动执行此操作? 最佳答案 Postman 有变量这个很好的特
Postman 允许使用 pre-defined variables 生成随机虚拟数据,例如,这个将被随机公司名称替换: {{$randomCompanyName}} 多次使用预定义变量会在每个请
我正在使用一个开放的 API。 但我只使用了 API 提供的响应中的一小部分数据。当我使用具有不同参数的 API 进行测试以查看响应时。 我不想每次发送请求时都看到整个 API 响应,我只想看到我感兴
我是一名优秀的程序员,十分优秀!