Ajaxupload and Spring MVC - ajax

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");

Related

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

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/

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 access previous SOAP headers in SoapInterceptor?

Here's my configuration:
A request is captured by my REST controller, I send data via SOAP to my webservice. Then I access some data sending SOAP request to another service and I return gathered data to the user sending the request.
Before sending SOAP request to external webservice I need to set some headers, so I have an interceptor that extends AbstractSoapInterceptor with Phase.PRE_PROTOCOL in constructor by webservice side.
Inside handleMessage() I create new headers and add to SoapMessage but... the data I need to set is inside REST request inside it's headers. So in order to get them I need to have access to HttpServletRequest and then I just get the header using HttpServletRequest#getHeader("header_name").
I've saw here I could just use #Context annotation but it's not available in my Spring (3.0.5) or maybe RESTEasy is something else and that question isn't connected with mine in anyway
I've tried this:
HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
and
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
but it's been a shot in the dark and it failed.
Edit:
Thanks to Abel ANEIROS I've added:
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
and was able to get HttpServletRequest from RequestContextHolder.getRequestAttributes() but apperently it's origin is from my webservice and not REST
Edit2:
The structure is different than I thought.
It's: Rest Controller -> MyWebService (SOAP) -> ExternalService (SOAP)
I've created another interceptor in controller side, added headers to SOAP message and now I'm trying to get the headers again in MyWebService side.

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.

Adding HTTP Headers in JSP

I am creating a Spring REST web services, that communicates with Android App and JSP web pages.
The method at my spring controller is like
#RequestMapping(method = RequestMethod.POST, value = "/login")
public ModelAndView userLogin(#RequestBody User user,
HttpServletRequest request){
//do something with user
}
Andoid App is able to access this method through adding request Headres like
"Content-Type" application/json , "Accept" application/json etc. Here the user information sent by android end is comes in request body. Thats ok..
But problem occurs when i POST the contents from my JSP page. I am not able to access the same userLogin method from jsp page with #RequestBody but when i replace it with #ModelAttribute it works for jsp page ...but then doesn't works for android app. Please tell me how can i solve this.
Make the JSP page do the same thing as the android app (posting as JSON) using JavaScript, or implement a second method in your Spring controller (userLogin2), mapped to a different URL, and use this URL in your JSP.

Resources