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();
}
}
如何使用其他完整服务Spring boot将文件或映像插入到db。我正在尝试restfull服务插入文件,但我有一个错误
无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:数字值中的意外字符(&#39; - &#39;(代码45)):预期数字(0- 9)跟随减号,表示有效数值 在[来源:java.io.PushbackInputStream@21d5ad7d; line:1,column:3];嵌套异常是com.fasterxml.jackson.core.JsonParseException:数字值中的意外字符(&#39; - &#39;(代码45)):跟随减号的预期数字(0-9),表示有效数值 在[来源:java.io.PushbackInputStream@21d5ad7d; line:1,column:3]
请求和响应如下图所示。
答案 0 :(得分:0)
您需要创建一个包含MultipartFile参数的REST方法。该对象具有getBytes()方法以及可用于获取数据的getInputStream()方法。然后,您可以创建对象并将其保存到存储库中。
请参阅https://spring.io/guides/gs/uploading-files/和http://www.concretepage.com/spring-4/spring-4-mvc-single-multiple-file-upload-example-with-tomcat作为如何使用Spring上传文件的良好参考。
以下是使用jQuery将文件上传到REST服务的前端示例。
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$.ajax({
url: "/restapi/requests/replay/upload",
type: "POST",
beforeSend: function (request)
{
request.setRequestHeader(header,token);
},
data: new FormData($("#upload-file-form")[0]),
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
success: function () {
// Handle upload success
addText( "File uploaded successfully.")
},
error: function () {
// Handle upload error
addText( "File upload error.")
}
});
然后这是Rest控制器的样子:
@RestController
public class ReplayRestController {
@Autowired
private ApplicationContext applicationContext;
@RequestMapping(value="/restapi/requests/replay/upload", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> processUpload(
@RequestParam("uploadfile") MultipartFile uploadfile,
@RequestParam("databaseWriteIdUpload") String databaseWriteId,
@RequestParam("recordsToUse")ReplayUpload.RecordsToUse recordsToUse
) {
try {
String fileName = uploadfile.getOriginalFilename();
if (databaseWriteId == null || "".equals(databaseWriteId)) {
databaseWriteId = fileName;
}
ReplayUpload replayUpload = applicationContext.getBean( ReplayUpload.class );
replayUpload.process( uploadfile.getInputStream(), databaseWriteId, recordsToUse );
}
catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(HttpStatus.OK); }
}