415 Unsupported Media Type while sending json file over REST Template

前端 未结 2 1992
名媛妹妹
名媛妹妹 2021-01-14 19:37

I am trying to send a json file over REST Template. When I send it via POST man as MULTIPART_FORM_DATA, it works fine. The name I am supposed to give is specific (lets say <

相关标签:
2条回答
  • 2021-01-14 20:06

    Finally I solved this issue. As mentioned in question, having different content type of multipart file while sending request from POSTMAN vs code is where I began with. I will explain in details if anyone has any questions.

        public Optional<JSONObject> save(byte[] multipartFile, String fileName) {
            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
            Resource content = new MultipartByteArrayResource(multipartFile , fileName);
    
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Resource> requestEntityBody = new HttpEntity<Resource>(content, headers);
            body.add("aaa", requestEntityBody);
            String result = "";
            JSONParser parser = new JSONParser();
            JSONObject json = null;
    
    
            HttpHeaders requestHeaders = new HttpHeaders();
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, requestHeaders);
            ResponseEntity<String> response = null;
            try {
               RestTemplate restTemplate = customizeRestTemplate(); //I have defined this in different config file in my actual code
               response =  restTemplate.exchange(url , HttpMethod.POST , requestEntity , String.class);
               result = (response != null && response.getBody() != null) ? response.getBody().toString() : result;
               json = (JSONObject) parser.parse(result);
               LOG.info( "Response:", response );
    
            } catch (Exception exception) {
                LOG.error("Error , exception);
                return Optional.empty();
            }
            return Optional.ofNullable(json);
        }
    
       public class MultipartByteArrayResource extends ByteArrayResource{
    
           private String fileName;
    
            public MultipartByteArrayResource(byte[] byteArray , String filename) {
                   super(byteArray);
                   this.fileName = filename;
               }
    
            public String getFilename() { 
                return fileName; 
              }
    
            public void setFilename(String fileName) {
                this.fileName= fileName;
             }
    
         }
    
          public RestTemplate customizeRestTemplate() {
    
                SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
                requestFactory.setReadTimeout(10000);
                requestFactory.setConnectTimeout(10000);
    
                RestTemplate restTemplate = new RestTemplate(requestFactory);
                List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
                messageConverters.add(new FormHttpMessageConverter());
                messageConverters.add(new StringHttpMessageConverter());
                restTemplate.setMessageConverters(messageConverters);
                return restTemplate;
            }
    
    }
    
    0 讨论(0)
  • 2021-01-14 20:06

    The server-side exception is produced by org.springframework.http.converter.json.MappingJackson2HttpMessageConverter. Jackson is a JSON library and MessageConverter are used by Spring to format requests and responses.

    Can it be that the client sends an "Accept: application/octet-stream" while the server has a @Produces(APPLICATION_JSON) annotation? That would mean that the server processes the request and only has problems sending the response. You could add some log.info() statements in the server to verify this.

    0 讨论(0)
提交回复
热议问题