如何使用 java 中的 Spring 数据 JPA 将附件(文件)添加到 couchbase 中的文档?
How can I add attachments (Files) to doccuments in couchbase using Spring data JPA in java?
这是我引用的链接 http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/attachment/index.html 但没有提及服务或 dao classes 中的用法。
spring doc for couchbase
这是我的道class
@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 中保存二进制 blob。
Couchbase lite 支持附件。
这是我引用的链接 http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/attachment/index.html 但没有提及服务或 dao classes 中的用法。 spring doc for couchbase
这是我的道class
@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 中保存二进制 blob。
Couchbase lite 支持附件。