gpt4 book ai didi

java - 如何使用 java 中的 Spring data JPA 将附件(文件)添加到 couchbase 中的文档?

转载 作者:行者123 更新时间:2023-11-30 07:52:24 25 4
gpt4 key购买 nike

这是我提到的链接http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/attachment/index.html但没有提及在 service 或 dao 类中的用法。 spring doc for couchbase

这是我的 dao 类

    @Repository
public interface UserRepository extends CrudRepository<User, String> {

}

下面是我的用户模型

    @JsonIgnoreProperties(ignoreUnknown=true)
@Document
public class User implements Serializable {
private static final long serialVersionUID = -6815079861643922076L;

@JsonProperty("docType")
private String docType = "users";
@Id
@JsonProperty("id")
private String id;
@JsonProperty("firstName")
private String firstName;
@JsonProperty("lastName")
private String lastName;
@JsonProperty("password")
private byte[] password;
public String getDocType() {
return docType;
}
public void setDocType(String docType) {
this.docType = docType;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public byte[] getPassword() {
return password;
}
public void setPassword(byte[] password) {
this.password = password;
}
}

这是我的服务实现

    @Service("userService")
public class UserServiceImpl implements UserService {
@Autowired(required=true)
private UserRepository userRepository;
public User findById(String id) throws Exception {
User user = userRepository.findOne(id.toLowerCase());
return user;
}
public User create(User user MultipartFile file) throws Exception {
String fileName = null;
if (!file.isEmpty()) {
try {
fileName = file.getOriginalFilename();
byte[] bytes = file.getBytes();
}catch (Exception ex) {
}
}
//i want to attach this file as attachment to this user in couch db
User returnUser = userRepository.save(user);
return returnUser;
}
public User update(User user MultipartFile file) throws Exception {
String fileName = null;
if (!file.isEmpty()) {
try {
fileName = file.getOriginalFilename();
byte[] bytes = file.getBytes();
}catch (Exception ex) {
}
}
//i want to attach this file as attachment to this user in couch db
User returnUser = userRepository.save(user);
return returnUser;
}
}

这是我的数据库配置

    @Configuration
@EnableCouchbaseRepositories(basePackages="com.repository")
public class DBConfig extends AbstractCouchbaseConfiguration {
@Autowired(required=true)
private PropertyFileReader propertyFileReader;
@Override
protected List<String> bootstrapHosts() {
List<String> couchbaseHostList = Arrays.asList(propertyFileReader.getCouchbaseHostList().split("\\s*,\\s*"));
return couchbaseHostList;
}
@Override
protected String getBucketName() {
String bucketName = propertyFileReader.getCouchbaseBucketName();
return bucketName;
}

@Override
protected String getBucketPassword() {
String bucketPassword = propertyFileReader.getCouchbaseBucketPassword();
logger.debug("bootstrapHosts() : bucketPassword={}", bucketPassword);
return bucketPassword;
}
public CustomConversions customConversions() {
return new CustomConversions(
Arrays.asList(StringToByteConverter.INSTANCE));
}
@ReadingConverter
public static enum StringToByteConverter implements Converter<String, byte[]> {
INSTANCE;
@Override
public byte[] convert(String source) {
return Base64.decodeBase64(source);
}
}

}

最佳答案

不幸的是,我认为您将 Couchbase Server 和 Couchbase lite 混淆了,它们是两种不同的产品。

您使用的 Spring 框架与 Couchbase Server 交互,但 Couchbase Server 不支持附件。不过,您可以在 Couchbase 中保存二进制 blob。

Couchbase lite 确实支持附件。

关于java - 如何使用 java 中的 Spring data JPA 将附件(文件)添加到 couchbase 中的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33178819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com