no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [multipart/form-data;boundary=XXX;charset=UTF-8] - spring-boot

I am doing multipart formdata post call from UI to upload a file.
From UI, the call goes to component A controller which you can consider it as a common component or bus which just read the stream pass the stream to other component B controller through rest call which uploads the file and sends back metadata about uploaded file in json format.
Issue: The call successfully goes till component B controller, file got uploaded but when the response of component B reaches component A, I am getting below error
"message" : "Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [multipart/form-data;boundary=HWhdmg6KNJw_kaP4wWnLyoAb9htc8StF4;charset=UTF-8]"
Can someone help pls?
Components A code
#PostMapping(value = "/v3/documents",consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity uploadMultipartDocument(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) {
ResponseEntity<?> responseEntity = null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set(HznAppsHeaderConstant.DOCUMENT_TYPE, HznAppsUtil.getHeaderFromCustomHeaderParameterProvider(customHeaderParameterProvider, HznAppsHeaderConstant.DOCUMENT_TYPE));
InputStream stream =null;
MultiValueMap<String, Object> params= new LinkedMultiValueMap<String, Object>();
try{
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterStream = upload.getItemIterator(req);
while (iterStream.hasNext()){
FileItemStream item = iterStream.next();
stream = item.openStream();
if (!item.isFormField() && item.getFieldName().equalsIgnoreCase("file")) {
InputStreamResource inputStream = new InputStreamResource(stream) {
#Override
public String getFilename() {
return item.getName();
}
#Override
public long contentLength() {
return -1;
}
};
params.add("file", inputStream);
break;
}
}
final HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
restTemplate.getInterceptors().clear();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate.setRequestFactory(requestFactory);
responseEntity = restTemplate.exchange("Component B URL", HttpMethod.POST, httpEntity, Object.class);
} catch (Exception e) {
e.printStackTrace();
}
return responseEntity;
}
Component B code
#RequestMapping(value = "/api/v3/documents", produces = {"application/json"}, consumes = {"multipart/form-data"}, method = RequestMethod.POST)
public ResponseEntity uploadMultipartDocument(HttpServletRequest request) {
ResponseDocument responseDocument = null;
try {
responseDocument = documentsService.uploadMultiPartDocument(request,documentType); // Doing Soap call to upload document and its works fine in this method
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>(responseDocument, HttpStatus.OK);
}
Thanks

Related

SpringBoot: HttpClientErrorException$BadRequest: 400: [no body] when calling restTemplate.postforEntity or restTemplate.exchange

I am trying to hit a restful endpoint from my springboot application using restTemplate.exchange and restTemplate.postForEntity and I am getting 400 Bad request [no body] exception.
I tried every possible solution to figure out the issue. From the same code I used HttpURLConnection to hit the endpoint and it works fine. I am able to get valid response. I also tried hitting the endpoint from Postman and it works fine
Below is endpoint code
#RestController
#RequestMapping("auth")
#Slf4j
public class AuthController {
#PostMapping(value = "/as/resourceOwner",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Token> getToken(#RequestParam(required = false)MultiValuedMap<?, ?> params) {
log.info("token endpoint");
return new ResponseEntity<>(new Token(), HttpStatus.OK);
}
}
The below code with HttpURLConnection class works fine
public class Test {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/fc-services-mock-auth/fmrco/as/resourceOwner");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
OutputStream writer = urlConnection.getOutputStream();
writer.write("a=a&b=b".getBytes(StandardCharsets.UTF_8));
writer.flush();
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
But the below code with restTemplate does not work
try {
HttpEntity httpEntity = new HttpEntity<>(createBody(), createHeaders());
ResponseEntity<String> response = restTemplate.exchange(new URI(endpointConfig.getTokenServiceUrl()),
HttpMethod.POST, httpEntity, String.class);
HttpEntity<MultiValueMap<String, String>> request =
new HttpEntity<>(createBody(), createHeaders());
ResponseEntity<TokenResponse> tokenResponse = restTemplate.postForEntity(
new URI(endpointConfig.getTokenServiceUrl()),
request,
TokenResponse.class);
logger.debug(" token process completed");
return tokenResponse.getBody();
} catch (Exception e) {
throw new TokenException(" token error: ", e);
}
private HttpHeaders createHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", MediaType.APPLICATION_FORM_URLENCODED_VALUE);
return headers;
}
private MultiValueMap createBody() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("client_id", applicationConfig.getTokenClientId());
map.add("client_secret", applicationConfig.getTokenClientSecret());
map.add("grant_type", "password");
map.add("username", applicationConfig.getTokenUsername());
map.add("password", applicationConfig.getTokenPassword());
return map;
}
Can anyone please tell me what is wrong here?
Additionally I have written another GET restful endpoint like below and while hitting the endpoint using exchange I still get the same error.
#GetMapping(value = "test", produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> test() {
return new ResponseEntity<>("Hello", HttpStatus.OK);
}
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_PLAIN));
HttpEntity<MultiValueMap<String, String>> entity =
new HttpEntity<>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(
"http://localhost:8080/context/path",
HttpMethod.GET, entity, String.class);
} catch (Exception e) {
System.out.println(e);
}
Also, intentionally I tried making a POST call to the get endpoint to see if it returns 405 Method not allowed. But to my wonder it still returned 400 Bad Request no body.
I finally figured it out. The restTemplate is configured to use a proxy. I removed the proxy and it is working fine now.
You can try something like:-
public void fetchAuthenticationToken() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("client_id", applicationConfig.getTokenClientId());
map.add("client_secret", applicationConfig.getTokenClientSecret());
map.add("grant_type", "client_credentials");
map.add("username", applicationConfig.getTokenUsername());
map.add("password", applicationConfig.getTokenPassword());
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
String tokenEndPoint = "{your endpoints URL}";
ResponseEntity<TokenResponse> responseEntity = testRestTemplate.exchange(tokenEndPoint,
HttpMethod.POST, entity, TokenResponse.class, new HashMap<String, String>());
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
TokenResponse token = responseEntity.getBody();
assertThat(token).isNotNull();
System.out.println("Auth Token value is "+ token)
}

How to get token from a REST service with Spring

The service provider supplies me with the header data: grant_type, Content-Type. And the body data: grant_type, username and password. Which I use in Postman where it generates OK token. But in the Spring application it generates an error HttpClientErrorException $ BadRequest: 400 Bad Request.
I have the class to set the body data:
public class BodyToken {
private String grant_type = "password";//set body data
private String username = "User";//set body data
private String password = "123";//set body data
private String access_token;
#JsonGetter("access_token")
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
#JsonGetter("grant_type")
public String getGrant_type() {
return grant_type;
}
#JsonGetter("username")
public String getUsername() {
return username;
}
#JsonGetter("password")
public String getPassword() {
return password;
}
}
This is the controller where the header data is set:
#PostMapping("/TokenGeneration")
#ResponseBody
public BodyToken TokenGeneration() throws IOException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.set("grant_type", "password");//set header data
headers.set("Content-Type", "application/x-www-form-urlencoded");//set header data
HttpEntity request = new HttpEntity(headers);
headers.add("User-Agent", "Spring's RestTemplate" );
ResponseEntity<BodyToken> response = restTemplate.exchange(
"https://sw/token",
HttpMethod.POST,
request,
BodyToken.class
);
try {
return response.getBody();
} catch (Exception e) {
BodyToken body = new BodyToken();
log.info(e.getMessage());
return body;
}
}
OK was solved with using the Class MultiValueMap and LinkedMultiValueMap. The credentials are added to this new object and it is sent together with the request:
#PostMapping("/TokenGeneration")
#ResponseBody
public BodyToken TokenGeneration() throws IOException {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.set("grant_type", "password");//set header data
headers.set("Content-Type", "application/x-www-form-urlencoded");//set header data
MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();//line solution
body.add("grant_type", "password");//line solution
body.add("username", "user");//line solution
body.add("password", "123");//line solution
HttpEntity request = new HttpEntity(body, headers);//and I add this body to HttpEntity
headers.add("User-Agent", "Spring's RestTemplate" );
ResponseEntity<BodyToken> response = restTemplate.exchange(
"https://sw/token",
HttpMethod.POST,
request,
BodyToken.class
);
try {
return response.getBody();
} catch (Exception e) {
BodyToken body = new BodyToken();
log.info(e.getMessage());
return body;
}
}

Spring REST - pass PDF through a request

I have got 2 Spring Boot Applications...
First application should generate a PDF and return it... Second Application is calling first application to get the pdf and return it to user...
Here i am generating a pdf.
#RequestMapping(value = "/html2pdf", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> report(#RequestParam() String htmlContent) {
try {
ByteArrayInputStream pdfDocument = pdfGenerator.generatePDF(htmlContent);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.add("Content-Disposition", "inline; filename=generated.pdf");
return ResponseEntity
.ok()
.headers(headers)
.body(new InputStreamResource(pdfDocument));
} catch (IOException e) {
return ResponseEntity.badRequest().build();
}
}
Here i am trying to make a call against first application and return the pdf...
#RequestMapping(value = "/pdfreport", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> report() {
ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();
List<MediaType> supportedApplicationTypes = new ArrayList<>();
MediaType pdfApplication = new MediaType("application", "pdf");
supportedApplicationTypes.add(pdfApplication);
resourceHttpMessageConverter.setSupportedMediaTypes(supportedApplicationTypes);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(resourceHttpMessageConverter);
RestTemplate pdfGenerator = new RestTemplate();
pdfGenerator.setMessageConverters(messageConverters);
ResponseEntity<InputStreamResource> response = pdfGenerator.getForEntity("http://localhost:1080/pdf-generator/html2pdf?htmlContent=<h2>HUHU</h2>", InputStreamResource.class);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "inline; filename=generated.pdf");
headers.setContentType(MediaType.APPLICATION_PDF);
return ResponseEntity
.ok()
.headers(headers)
.body(response.getBody());
}
in this constellation i get following error:
java.io.IOException: stream is closed
I have already tried it with ResponseExtractor...
What is the problem here? How can i solve it more easy?
Thanks
InputStreamResource Should only be used if no other specific Resource implementation is applicable. In particular, prefer ByteArrayResource or any of the file-based Resource implementations where possible.
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/io/InputStreamResource.html

java.lang.UnsupportedOperationException:null when setting header using response entity in spring

I am calling a web service which is returning me ResponseEntity object .Now I am setting location header after getting HttpHeaders information from ResponseEntity.
responseEntity.getHeaders().setLocation(uriBuilder.path("/empl/{id}").buildAndExpand(empl.getEmpId()).toUri());
The above line of code is throwing java.lang.UnsupportedOperationException as headers is not modifiable. How can i set the location header in this particular case?
Here is what I suggest to use:
#GetMapping(value = "/get-products-detail/", produces = "application/json; charset=UTF-8")
public ResponseEntity<Object> getProductsDetail(#RequestParam Long userToken, #RequestParam Long productId) {
try {
return ResponseEntity.ok().body(digitoonContentService.getProductsDetails(userToken, productId));
} catch (InvalidTokenException e) {
logger.info("token is null or invalid", e);
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add("invalid_token", "true");
ErrorDTO errorDTO = new ErrorDTO(HttpStatus.UNAUTHORIZED, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
return new ResponseEntity<Object>(errorDTO, headers, HttpStatus.UNAUTHORIZED);
}

How to do file upload with Spring REST API and Test it with RestTemplate

I want to,
1) Implement some REST service method with Spring Rest API for upload some files from my remote web client.
2) Test that with my RestTemplate based remote web client.
If any one has some idea please help me. Thanks.
Some of my Spring REST API base methods are as below,
#RequestMapping(value="user/create/{userRoleName}", method=RequestMethod.POST)
public #ResponseBody User create(#RequestBody User user, #PathVariable String userRoleName, HttpServletResponse response) { }
Some of my remote client's Spring RestTemplate base codes are as below,
Map<String, String> vars = new HashMap<String, String>();
vars.put("userRoleName", userRoleName);
ResponseEntity<User> REcreateUser = restTemplate.postForEntity(IMC_LAB_SKELETON_URL + "/user/create/{userRoleName}", newUser, User.class, vars);
User createUser = REcreateUser.getBody();
Try this:
public class FileUploadService {
#POST
#Path("/file")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(Attachment attachment,#Context HttpServletRequest request) {
DataHandler handler = attachment.getDataHandler();
try {
InputStream stream = handler.getInputStream();
MultivaluedMap map = attachment.getHeaders();
OutputStream out = new FileOutputStream(new File(getFileName(map)));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
stream.close();
out.flush();
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return Response.ok("file uploaded").build();
}
This can easily be tested with RestTemplate.
Imports:
MediaType
Attachment
MultivaluedMap
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
//Json Payload as String
bodyMap.add("payload", payload);
for (File file : fileArray) {
bodyMap.add("file", new FileSystemResource(file));
}
HttpEntity<MultiValueMap<String, ?>> entity = new
HttpEntity<MultiValueMap<String, ?>>(bodyMap, headers);

Resources