How to post multipart content type in single request body using spring and restful service - spring

We have a requirement to post a multiformats request type to send to a resful api..I am a beginer and need help with a sample program hiw to post multiple requests with different format like json and pdf in single http request so that I can get a response back?can anyone please post a sample code how to implement multi part content type?

I've had already developed an upload microservice using multipart/form-data requests. The example bellow is a simple endpoint that accepts multiple files using multipart/form-data. You only need to use the spring MultipartFile class as the argument.
#PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity upload(
#RequestPart(value = "files") MultipartFile[] files) {
return ResponseEntity.ok().build();
}
For more details I highly recommend reading the spring official guide in uploading files: https://spring.io/guides/gs/uploading-files/

Related

Spring RestController endpoint that consumes and produces a String value returning "HTTP Status 415 – Unsupported Media Type"

I'm trying to setup a fairly simple endpoint that takes in String and produces a String. I've simplified it down to:
#RequestMapping(value="/foo",
method = RequestMethod.GET,
consumes = MediaType.TEXT_PLAIN_VALUE,
produces= MediaType.TEXT_PLAIN_VALUE)
public String foo(#RequestBody String data) {
return data;
}
I'm testing with Postman and am just getting "HTTP Status 415 – Unsupported Media Type" back. This app is running on Spring 4.1.3. I copied this endpoint into another project I have in Spring Boot, using my same Postman request (just modified the host), and it works there. So there appears to be some issue with the version of Spring this project uses. As a work-around I created a simple wrapper entity (POJO that only consists of a single String) and changed the endpoint to consume JSON/this POJO. Does anyone know specifically why it doesn't work as above and/or know of a workaround that'll let me send and receive a plain text String with this version of Spring?

how to send multipart data and json data together in one request using curl or postman

I have a case where I have to save json data and upload a file in one request.
this is how I am doing in spring
public void profile (#RequestBody #Validated #Valid ProfileDTO profileDTO,
#RequestPart("file") #Valid MultipartFile file){
/// saving to db
}
Now how can I test this using curl and postman?
after:
https://github.com/postmanlabs/postman-app-support/issues/1104
You can not do this in Postman yet (postman devs works on this feature).
Other users send json data as a file, but it is only a work around available in Postman.

How to send POST requests to a Spring Boot REST API using Postman form-data?

I'm trying a very simple Spring Boot + MySql app using STS: https://github.com/acenelio/person01
Posting a new Person using raw body works fine. However, when trying to post using form-data, I get either a 400 HttpMessageNotReadableException or 415 HttpMediaTypeNotSupportedException error, depending on which Content-type I set on Headers.
I provided some Postman printscreens here: https://sites.google.com/site/stackoverflownelioalves/arqs
What am I missing? I'm kinda new on Postman and Spring Boot. Thanks.
You can use the #RequestBody annotation:
#RequestMapping("/addInvestorProfile")
public #ResponseBody Boolean addInvestorProfile(#RequestBody() InvestorProfile profile
, #RequestParam(value = "sessionId") String sessionId)
{
.... your content
return true;
}
I am not sure which version of your spring boot. First of all, almost all spring mvc or spring boot controller method will accept all request content-type when you are not add consumer param to #RequestMapping annotation. So, maybe there is some wrong usage of postman.And I am work with spring boot & postman well.
try to compare postman behavior by copy request to curl
Usually post a request use curl looks like:
curl -XPOST UrlOfYourSpringBootEndPoint -d'body of your post'
more detail of curl here.

How can I see the json coming from the client when using Spring-MVC?

A client software is trying to access my Spring-MVC rest server, but it's getting a 400 (Bad Request) response every time. I know my server is fine (it's in use by many other clients), but I cannot debug the client application, so I cannot see what it is sending.
Is there a way for me to see what JSON I am receiving before Spring tries to convert it to an entity and fails? It's okay if I can only do this at debug time, I just need to be able to give support to this application's creators.
Just in case, here is the spring-mvc controller method:
#Named
#RequestMapping(value = "/taskmanager/task")
public class TaskManagerTaskRest {
#RequestMapping(value = "", method = RequestMethod.POST)
#ResponseBody
public void createTask(#RequestBody Task task, HttpServletRequest request,
HttpServletResponse response) throws CalabacinException {
// This code never gets executed because the Task json is invalid, but I don't know how I could see it.
...
...
}
}
Try to use Fiddler. It will help you to catch HTTP requests/responses. You will be able to see your JSON.
You can create and use a AbstractRequestLoggingFilter filter implementation and conditionally log the relevant parts of the request. You should use ContentCachingRequestWrapper to wrap the request.

Ajaxupload and Spring MVC

If you upload a file via a normal form, then it works.
If you load the file / files with ajaxupload nothing works.
Error:
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
Code:
#RequestMapping (value = "/ upload", method = RequestMethod.POST)
public void upload (# RequestParam MultipartFile file,
HttpServletRequest request, HttpServletResponse response)
Purpose - Multibooting files with ajax, can anyone have a working example for the Spring.
I have a separate servlet that receives HttpServletRequest and parses everything is fine. On the client side ajaxupload.
If you try a simple Spring MVC transfer request in this class, he refuses to work, arguing that the request is not multipart. Spring is obtained as a sawing the original request?
Please change fileupload.js , search and comment out the line which has "application/octet-stream"
and add following line :
xhr.setRequestHeader("Content-Type", "multipart/form-data");

Resources