Reputation: 15
Here's are the links I referred to http://developer.couchbase.com/documentation/mobile/current/develop/guides/couchbase-lite/native-api/attachment/index.html but there is no mention about usage in service or dao classes. spring doc for couchbase
This is my dao class
@Repository
public interface UserRepository extends CrudRepository<User, String> {
}
Below is my user model
@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;
}
}
this is my service implementation
@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;
}
}
And here is my dbconfig
@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);
}
}
}
Upvotes: 1
Views: 567
Reputation: 1195
Unfortunately I think you are getting Couchbase Server and Couchbase lite mixed up they are two different products.
The spring framework you are using interacts with Couchbase Server, which does not support attachments. You can however save binary blobs in Couchbase.
Couchbase lite does support attachments.
Upvotes: 1