send multipart response in spring boot - spring-boot

I am working on apis which is developed in spring boot. Now I have one API in which I have to send response which contains one binary file and and xml.
Both will be seperated by multipart boundary.
So is there any way to do this?

In spring boot try following the way to send a response in multipart.
#RequestMapping(method = { RequestMethod.GET },value = "/multipartdata",produces=MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<MultiValueMap<String, Object>> gerMultipartData()
throws Exception {
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("first_name", "ganesh");
formData.add("last_name", "patil");
formData.add("file-data_1", new FileSystemResource("C:\Users\ganesh\img\logo.png"));
formData.add("file-data_2", new FileSystemResource("C:\Users\ganeshg\Desktop\download.jpg"));
formData.add("file-data_3", new FileSystemResource("C:\Users\ganeshg\Desktop\odstext.txt"));
formData.add("file-data_4", new FileSystemResource("D:\Agent\152845.docx"));
formData.add("file-data_5", new FileSystemResource("D:\testxls.xlsx"));
return new ResponseEntity<MultiValueMap<String, Object>>(formData, HttpStatus.OK);
}

Related

REST API call from spring boot not working

I am trying to fetch live data from NSE options trading. Below code is not working and the request made is stuck without any response.
Any workaround on this?
public void getLiveBankNiftyData() {
String RESOURCE_PATH = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY";
ResponseEntity<Object[]> responseEntity = restTemplate.getForEntity(RESOURCE_PATH, Object[].class);
Object[] objects = responseEntity.getBody();
}
i tried this
// request url
String url = "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY";
// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();
// make an HTTP GET request
String json = restTemplate.getForObject(url, String.class);
// print json
System.out.println(json);
I found a way out. Instead of using RestTemplate I used WebClient and this solved the issue.

why my spring webflux vs spring mvc load test throughput is same?

We are comparing the throughput of the spring webflux vs spring mvc for one of our project.
We have service1 and service2. Service1 prepares content and posts it to service2 using webclient. The code below is from service1 which posts content to service2.
Note: Service 2 takes 500ms to process a single request.
Spring Webflux
#PutMapping(path = "/")
public Mono<String> create() throws Exception {
ClassPathResource classpathResource = new ClassPathResource("/file.zip");
MultipartBodyBuilder multipartBodyBuilder = new MultipartBodyBuilder();
multipartBodyBuilder.part("key", "value");
multipartBodyBuilder.part("file", classpathResource, MediaType.APPLICATION_OCTET_STREAM);
MultiValueMap<String, HttpEntity<?>> multiValueMap = multipartBodyBuilder.build();
return WebClient.create()
.post()
.uri(uri)
.contentType(MediaType.MULTIPART_FORM_DATA)
.headers(httpHeaders -> httpHeaders.setBearerAuth(token))
.body(BodyInserters.fromMultipartData(multiValueMap))
.retrieve()
.bodyToMono(String.class);
}
Spring MVC
#PutMapping(path = "/")
public ResponseEntity<String> create() throws Exception {
String response = null;
ClassPathResource classpathResource = new ClassPathResource("/file.zip");
try (InputStream zipInputStream = new BufferedInputStream(classpathResource.getInputStream())) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setBearerAuth(token);
LinkedMultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
ContentDisposition contentDisposition = ContentDisposition.builder("form-data").name("file").filename("file")
.build();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
fileMap.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
HttpEntity<InputStreamResource> sipEntity = new HttpEntity<InputStreamResource>(
new InputStreamResource(zipInputStream), fileMap);
LinkedMultiValueMap<String, String> formatfileMap = new LinkedMultiValueMap<>();
contentDisposition = ContentDisposition.builder("form-data").name("key")
.build();
formatfileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
HttpEntity<String> formatEntity = new HttpEntity<String>("value", formatfileMap);
LinkedMultiValueMap<String, Object> linkedMultiValueMap = new LinkedMultiValueMap<String, Object>();
linkedMultiValueMap.add("file", sipEntity);
linkedMultiValueMap.add("key", formatEntity);
HttpEntity<LinkedMultiValueMap<String, Object>> request = new HttpEntity<LinkedMultiValueMap<String, Object>>(
linkedMultiValueMap, headers);
response = restTemplate.postForObject(ingestUrl, request, String.class);
}
return new ResponseEntity<String>(response, HttpStatus.OK);
}
We did a load test with 200, 300, 500, 1000 concurrent users using jmeter. In all the cases we have received same throughput for both frameworks. Are we doing something wrong here?
I have captured load test stats using Gatling for 1000 users for both mvc and reactive.
Webflux - 1000 users
Spring webflux with 1000 concurrent users
MVC - 1000 users
Spring MVC with 1000 concurrent users
I wouldn't say you did something wrong, rather this specific use case is not suitable for a hard comparison. Both endpoints rely on first loading something from a disk and then making an HTTP call.
The limiting factor here (I guess) is for both the network call, which takes the same time to finish nonetheless you use WebFlux or the MVC.
The benefit you get from WebFlux is the non-blocking behaviour, which scales better with fewer resources under load.
Something similar was already answered quite well on Quora: https://www.quora.com/Does-Spring-WebFlux-perform-better-than-Spring-MVC

Sending a multipart request using RestTemplate

I want to make a multipart request to some external API (created using Spring Boot) but all I get is Required request part 'file' is not present.
I know the source code of the external API but I can't modify it. It looks like this:
#PostMapping("/upload")
public ResponseEntity handleFileUpload(#RequestParam("file") MultipartFile file){
return ResponseEntity.ok().build();
}
And from my application I create and send requests exactly like on the following snippet:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body
= new LinkedMultiValueMap<>();
body.add("file", "dupa".getBytes());
HttpEntity<MultiValueMap<String, Object>> requestEntity
= new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate
.postForEntity("http://api:8080/upload", requestEntity, String.class);
return response.getBody();
What's the reason it doesn't work? The above code rewritten using Apache HttpClient works like charm.
You basically have two options, the solution with byte array:
map.add("file", new ByteArrayResource(byteArrayContent) {
#Override
public String getFilename() {
return "yourFilename";
}
});
I remember having a problem with just adding a byte array, so you need to have a filename too and use ByteArrayResource.
Or adding a File:
map.add("file", new FileSystemResource(file));

How to send body content as raw JSON and not form-data in Spring Boot RestTemplate

I have a spring boot application and trying to invoke a rest service of another company by using RestTemplate.
The remote Rest Service required multiple header and body content as Raw JSON.
Here is the sample required body request :
{
"amount": "10000",
"destinationNumber": "365412"
}
But my request body generate like this :
{
amount= [10000],
destinationNumber= [365412]
}
I've done like this :
String BASE_URI = "http://server.com/sericeX";
RestTemplate template = new RestTemplate();
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization","Some token");
headers.add("Content-Type", "application/json");
MultiValueMap<String, String> bodyParam = new LinkedMultiValueMap<>();
bodyParam.add("amount", request.getAmount());
bodyParam.add("destinationNumber",request.getDestinationNumber());
HttpEntity entity = new HttpEntity(bodyParam,headers);
ResponseEntity<TransferEntity> responseEntity = template.exchange(BASE_URI, HttpMethod.POST, entity,TransferEntity.class);
TransferEntity transferEntity = responseEntity.getBody();
Could you please tell me how can i generate body request as JSON ?
Thanks to #Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:
HashMap<String, String> bodyParam = new HashMap<>();
bodyParam.put("amount", request.getAmount());
bodyParam.put("destinationNumber",request.getDestinationNumber());

Post data using Spring RestTemplate

I am trying to post data using Spring RestTemplate as below:
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
parameters.add("name1", "value1");
parameters.add("name2", "value2");
HttpMessageConverter<String> stringConverter = new StringHttpMessageConverter();
FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
List<HttpMessageConverter<?>> msgConverters = new ArrayList<HttpMessageConverter<?>>();
msgConverters.add(formConverter);
msgConverters.add(stringConverter);
restTemplate.setMessageConverters(msgConverters);
String xml = restTemplate.postForObject(myurl, parameters, String.class);
On the server part, I am using a simple servlet to handle request as follow:
String name1 = request.getParameter("name1");
The server returns the xml as String.
When I used HashMap instead of MultiValueMap without Converter, the parameters are null on the server side. But after using the above code, I am getting error
Cannot extract response: no Content-Type found
Can you plz provide me a simple example to achieve what I want.
Here is what I used to format data for the Spring POST:
//FormHttpMessageConverter
is used to construct form parameters to POST on the URI
HttpMessageConverter<?> formHttpMessageConverter = new FormHttpMessageConverter();
HttpMessageConverter<?> stringHttpMessageConverter = new StringHttpMessageConverter();
List<HttpMessageConverter> msgConverters = new ArrayList<HttpMessageConverter>();
msgConverters.add(formHttpMessageConverter);
msgConverters.add(stringHttpMessageConverter);
// Prepare acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.ALL);
// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
HttpEntity<MultiValueMap<String,String>> httpEntity = new HttpEntity<MultiValueMap<String,String>>(map,headers);
ResponseEntity<String> resp = restTemplate.exchange("https://risk.XXXX.XXXXXX.net",HttpMethod.POST,httpEntity,String.class);

Resources