2017-09-18 1 views
1

I Problem habe den Aufbau eine api, deren Antwort ist mehrteiliger/Form-Daten mit application/json InhaltFederauflage eines Web Service zurückkehrt multipart/Form-Daten mit application/json content-type

Beispiel:

http://localhost:8080/getData 

sollte

--HaRZrgSMRFElYDqkcBMfTMp3BUMHKQAtP 
Content-Disposition: form-data; name="response" 
Content-Type: application/json 

[{"name":"xyz"}] 
--HaRZrgSMRFElYDqkcBMfTMp3BUMHKQAtP-- 

der aktuelle Code-Schnipsel Rückkehr ist

@RequestMapping(value="/getData", method=RequestMethod.GET, 
produces=MediaType.MULTIPART_FORM_DATA_VALUE) 
public MultipartFile getMultipartAsFileAsObject() throws Exception { 
    ClassLoader classLoader = getClass().getClassLoader(); 
    File file = new File(classLoader.getResource("sample.json").getFile()); 
    String readFile = readFile("sample.json"); 
    DiskFileItem fileItem = new DiskFileItem("file", "application/json", false, "response", (int) file.length() , file); 
    fileItem.getOutputStream(); 
    MultipartFile multipartFile = new CommonsMultipartFile(fileItem); 
    return multipartFile; 
} 

und die Antwort, die ich bekomme, ist {} ein leeres Json-Objekt. kann jemand lassen Sie mich wissen, wo ich falsch werde

Antwort

1

Ich habe die Lösung herausgefunden, es zu veröffentlichen, damit es für andere

@RequestMapping(method = { RequestMethod.GET }, value = "/getData", produces = 
MediaType.MULTIPART_FORM_DATA_VALUE) 
public ResponseEntity<MultiValueMap<String, Object>> getData() { 

    s= "[{\"sample\": \"sample\"}]"; 
    JsonArray ja = (new JsonParser()).parse(s).getAsJsonArray(); 
    MultiValueMap<String, Object> mpr = new LinkedMultiValueMap<String, Object>(); 
    HttpHeaders xHeader = new HttpHeaders(); 
    xHeader.setContentType(MediaType.APPLICATION_JSON); 
    HttpEntity<String> xPart = new HttpEntity<String>(ja.toString(), xHeader); 
    mpr.add("response", xPart); 
    return new ResponseEntity<MultiValueMap<String, Object>>(mpr, 
      HttpStatus.OK); 
} 

Antwort

--57XYHIgdIhRSOYu6TZA-ybSppMuAtcN3 
Content-Disposition: form-data; name="response" 
Content-Type: application/json 
Content-Length: 1186 

[{"sample": "sample"}] 
--57XYHIgdIhRSOYu6TZA-ybSppMuAtcN3-- 
hilfreich sein könnten