My RestPictureServices Class
@Service
@TestProfile
public class RestPictureServices implements SahaPictureServices {
@Autowired
private PictureRepository pictureRepository;
@Autowired
private DozerBeanMapper mapper;
@Override
public Collection<SahaPicture> pictures() {
// TODO Auto-generated method stub
return null;
}
@Override
public SahaPicture pictureOfSaha(Long sahaId) {
// TODO Auto-generated method stub
return null;
}
@Override
public SahaPicture save(SahaPicture picture) {
SahaPictureEntity pictureEntity=new SahaPictureEntity();
mapper.map(picture, pictureEntity);
pictureRepository.save(pictureEntity);
SahaPicture savedPicture=new SahaPicture();
mapper.map(pictureEntity, savedPicture);
return savedPicture;
}
@Override
public Boolean delete(Long id) {
// TODO Auto-generated method stub
return true;
}
@Override
public SahaPicture update(Long id, SahaPicture picture) {
// TODO Auto-generated method stub
return null;
}
}
My SahaPictureController class
@JsonRestController
@RequestMapping(path = "/saha/picture")
public class PictureController {
@Autowired
@Qualifier("restPictureServices")
private SahaPictureServices pictureServices;
@RequestMapping(method = RequestMethod.POST)
public SahaPicture singleSave(@RequestBody SahaPicture picture) {
return pictureServices.save(picture);
}
}
My PictureSahaRepository interface
public interface PictureRepository extends CrudRepository<SahaPictureEntity,Long> {
}
My picture Model class
public class SahaPicture {
private MultipartFile file;
//getter and setter methods
}
This is SahaPictureEntity class
@Entity
@Table(name="SahaPicture")
public class SahaPictureEntity {
@Id
@GeneratedValue
private Long id;
@Column
@Lob
private MultipartFile file;
//getter and setter methods
}
My JsonRestController Annotation
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonRestController {
}
My Services Interface
public interface SahaPictureServices {
Collection<SahaPicture> pictures();
SahaPicture pictureOfSaha(Long sahaId);
SahaPicture save(SahaPicture picture);
Boolean delete(Long id);
SahaPicture update(Long id, SahaPicture picture);
}
My Service Configuration Class using dozer mapping jar.
@Configuration
public class ServiceConfiguration {
@Bean
public DozerBeanMapper mapper() {
return new DozerBeanMapper();
}
}
How can I insert a file or an image to db with rest full services Spring boot. I am trying to restfull services to insert a file but I have got an error
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value at [Source: java.io.PushbackInputStream@21d5ad7d; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value at [Source: java.io.PushbackInputStream@21d5ad7d; line: 1, column: 3]
The request and response is in below picture.