2016-04-24 115 views
0
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]

請求和響應是在以下的畫面。

enter image description here

回答

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.") 
      } 
     }); 

然後這裏是休息控制器的樣子:

@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); } 
} 
+0

我只想做CRUD例如上傳文件,休息方法。我不想用前端上傳文件。 – user3233571

+0

重要的一點是如何在休息服務中發佈字節數據。 – user3233571

+0

「我不想用前端上傳文件」是什麼意思?你打算如何獲得數據到服務器?據我所知,不使用MultipartFile參數上傳文件是不可能的。 –