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();
}
}
如何插入文件或圖像到數據庫與休息全服務春季啓動。我嘗試重新安裝服務來插入文件,但出現錯誤RestFull服務文件上傳
無法讀取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:無法讀取文檔:意外字符(' - '(代碼45 ))中的數字值:預期數字(0-9),以遵循減號,對於在[來源有效的數值 :[email protected];行:1,列:3];嵌套異常是com.fasterxml.jackson.core.JsonParseException:意外字符(「 - 」(代碼45))在數值:預期數字(0-9),以遵循減號,對於在[來源有效的數值 :JAVA .io.PushbackInputStream @ 21d5ad7d;行:1,柱:3]
請求和響應是在以下的畫面。
我只想做CRUD例如上傳文件,休息方法。我不想用前端上傳文件。 – user3233571
重要的一點是如何在休息服務中發佈字節數據。 – user3233571
「我不想用前端上傳文件」是什麼意思?你打算如何獲得數據到服務器?據我所知,不使用MultipartFile參數上傳文件是不可能的。 –