spring post mulipart and body - spring

I am trying to upload a file which works fine, the method looks like this:
#PostMapping("/upload")
public FileResponse uploadFile(#RequestPart("file") MultipartFile file) {
...
But when I try to upload another json beside it it gets error:
#PostMapping("/upload")
public FileResponse uploadFile(#RequestPart("file") MultipartFile file, #RequestBody UserDTO userDTO) {
...
I need UserDTO to verify the user.
Here is my postman snap:

You won't be able to get both RequestBody and RequestPart in the same controller for multipart request. The workaround I used was to send the object as string and convert it back to object in controller. Eg. below:
#PostMapping(value = "/upload", consumes = {"multipart/form-data"})
public ResponseEntity<Object> upload(
#RequestParam(required = false, value = "document") MultipartFile document,
#Valid #RequestParam("userDTOString") String userDTOString) throws JSONException {
UserDTO userDTO = new ObjectMapper().readValue(userDTOString, UserDTO.class);
return documentService.uploadFile(document, userDTO);
}

Related

SpringBoot Rest controller MultipartFile obj receives null when binary file type is being sent from client

I am trying to upload png and jpeg file type only.
Here is my controller:
#PostMapping(path ="/upload/image", consumes = { MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<SSSResponse> uploadImage(#ModelAttribute UserDefinedFieldImageUploadDto image,
#RequestHeader HttpHeaders headers){
Object response = userDefinedFieldService.uploadImage(image,headers);
return ResponseEntity.status(HttpStatus.OK)
.body(buildSSSResponseResponse(HttpStatus.OK.toString(), Constants.SUCCESS, response));
}
Here is my dto class:
#Data
public class UserDefinedFieldImageUploadDto {
private MultipartFile multipartFile;
private int clientId;
}

Spring boot: Sending a JSON to a post request that uses a model as a param

Lets say I have a predefined post mapping as such:
#PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> addVal(#RequestBody final valDetail newVal) {
//Do Stuff
}
and the valDetail object as follows:
#Data
#Component
#Entity
#Table(name = "val_portal")
public class valDetail {
#Id
#Column(name = "valcode")
private String valCode;
#Column(name = "valname")
private String valName;
}
How would I go about actually sending JSON values from a separate service to this /add endpoint so that they are properly received as a valDetail object?
Currently I tried this implementation but I keep getting a 415 response code.
JSONObject valDetail = new JSONObject();
valDetail.put("valCode",request.getAppCode().toLowerCase());
valDetail.put("valName", request.getProjectName());
String accessToken = this.jwtUtility.retrieveToken().get("access_token").toString();
HttpHeaders authHeaders = new HttpHeaders();
authHeaders.setBearerAuth(accessToken);
authHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(valDetail.toString(), authHeaders);
ResponseEntity<String> loginResponse = restTemplate.exchange(uri,
HttpMethod.POST,
entity,
String.class);
If you want to pass data as json you don't want to take Model try to use #ResponseBody annotation to transfer data through json.
#PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public ResponseEntity<String> addVal(#RequestBody final valDetail newVal) {
//Do Stuff
}

Can i send a error-map along with a response object in response? What is the proper way?

I have a spring rest end point which accepts a user object and returns another userobject as response. My controller method looks like:
#PostMapping
public UserResponse createUser(#RequestBody UserDetailsRequestModel userDetails)
throws Exception {
UserRest userResponse= new UserRest();
ModelMapper modelMapper = new ModelMapper();
UserDto userDto = modelMapper.map(userDetails, UserDto.class);
userDto.setRoles(new HashSet<>(Arrays.asList(Roles.ROLE_USER.name())));
UserDto createdUser = userService.createUser(userDto);
returnValue = modelMapper.map(createdUser, UserResponse.class);
return userResponse;
}
My userResponse class looks like
public class UserRest {
private String userId;
private String firstName;
private String lastName;
private String email;
....getters and setters
And this flow is working fine. But now I need to add validation to createUser method (JSR 303) to check if incoming JSON fields are ok. For this I am trying to add below code in my controller
#PostMapping
public UserResponse createUser(#Valid #RequestBody UserDetailsRequestModel userDetails, BindingResult result){
if(result.hasErrors()){
Map<String, String> errorMap = new HashMap<>();
for(FieldError error: result.getFieldErrors()){
errorMap.put(error.getField(), error.getDefaultMessage());
}
**return new ResponseEntity<Map<String, String>>(errorMap, HttpStatus.BAD_REQUEST);**
}
UserRest userResponse= new UserRest();
ModelMapper modelMapper = new ModelMapper();
UserDto userDto = modelMapper.map(userDetails, UserDto.class);
userDto.setRoles(new HashSet<>(Arrays.asList(Roles.ROLE_USER.name())));
UserDto createdUser = userService.createUser(userDto);
returnValue = modelMapper.map(createdUser, UserResponse.class);
return **userResponse**;
The obvious problem in my code is that I can't convert from ResponseEntity<Map<String,String>> to UserResponse object.
Is there a proper way of doing this ? so that I can send errors(if any) or the UserResponse object if there are no errors within the same controller method?
Return a type of ResponseEntity<?>
#PostMapping
public ResponseEntity<?> createUser(#Valid #RequestBody UserDetailsRequestModel userDetails, BindingResult result){
if(result.hasErrors()){
...
return ResponseEntity.badRequest().body(errorMap);
}
...
return ResponseEntity.ok(userRequest);
}
You can return an HttpEntity<?> generic object from a controller method accepting both response type.
You have to wrap the UserResponse response around a ResponseEntity object

Accessing Rest URL from inside a servlet

I am trying to create a servelet that will accept a post from any url so I have created the following RestController:
#RequestMapping( method=RequestMethod.POST, value="/**")
public #ResponseBody String processPost(#RequestBody String msg)
{
// work done in here
return msg;
}
Is there a way I can find out the url that was used from inside the processPost method?
Spring allows flexible method signatures so you can use the HttpServletRequest object directly:
#RequestMapping(method = RequestMethod.POST, value="/**")
public #ResponseBody String processPost(#RequestBody String msg,
HttpServletRequest request) {
String thePath = request.getRequestURI();
return msg;
}

#RequestPart with mixed multipart request, Spring MVC 3.2

I'm developing a RESTful service based on Spring 3.2. I'm facing a problem with a controller handling mixed multipart HTTP request, with a Second part with XMLor JSON formatted data and a second part with a Image file .
I am using #RequestPart annotation for receiving the request
#RequestMapping(value = "/User/Image", method = RequestMethod.POST, consumes = {"multipart/mixed"},produces="applcation/json")
public
ResponseEntity<List<Map<String, String>>> createUser(
#RequestPart("file") MultipartFile file, #RequestPart(required=false) User user) {
System.out.println("file" + file);
System.out.println("user " + user);
System.out.println("received file with original filename: "
+ file.getOriginalFilename());
// List<MultipartFile> files = uploadForm.getFiles();
List<Map<String, String>> response = new ArrayList<Map<String, String>>();
Map<String, String> responseMap = new HashMap<String, String>();
List<String> fileNames = new ArrayList<String>();
if (null != file) {
// for (MultipartFile multipartFile : files) {
String fileName = file.getOriginalFilename();
fileNames.add(fileName);
try {
file.transferTo(new File("C:/" + file.getOriginalFilename()));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
responseMap.put("displayText", file.getOriginalFilename());
responseMap.put("fileSize", "" + file.getSize());
response.add(responseMap);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept", "application/json");
return new ResponseEntity<List<Map<String, String>>>(response,
httpHeaders, HttpStatus.OK);
}
User.java will be like this-
#XmlRootElement(name = "User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int userId;
private String name;
private String email;
private String company;
private String gender;
//getter setter of the data members
}
To my understanding, using the #RequestPart annotation I would expect the XML multipart section to be evaluated depending on its Content-Type and finally un-marshalled into my User class (I'm using Jaxb2, the marshaller/unmarhaller is properly configured in the application context and the procedure is working fine for all the other controller methods when I pass the XML data as body and use the #RequestBody annotation).
But what is actually happening is that, although the file is correctly found and parsed as MultipartFile, the "user" part is never seen and the request is always failing, not matching the controller method signature.
I reproduced the problem with several clients type and I am confident the format of the multipart request is ok.
Please help me to solve this issue, Maybe some workaround will be there to receive mixed/multipart request.
Thanks and Regards,
Raghvendra
I have managed to solve the problem
Endpoint example:
#PostMapping("/")
public Document create(#RequestPart Document document,
#RequestPart(required = false) MultipartFile file) {
log.debug("#create: document({}), file({})", delegation, file);
//custom logic
return document;
}
Exception:
"error_message": "Content type 'application/octet-stream' not supported"
Exception is thrown from the next method:
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(HttpInputMessage,MethodParameter,Type)
Solution:
We have to create custom converter #Component, which implements HttpMessageConverter or HttpMessageConverter and knows about MediaType.APPLICATION_OCTET_STREAM. For simple workaround it's enough to extend AbstractJackson2HttpMessageConverter
#Component
public class MultipartJackson2HttpMessageConverter extends AbstractJackson2HttpMessageConverter {
/**
* Converter for support http request with header Content-Type: multipart/form-data
*/
public MultipartJackson2HttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
}
#Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}
#Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
return false;
}
#Override
protected boolean canWrite(MediaType mediaType) {
return false;
}
}
Not sure if you had fixed your problem, but I also had a similar problem where my JSON object was not getting picked up by my controller when mixing #RequestPart and MultipartFile together.
The method signature for your call looks correct:
public ResponseEntity<List<Map<String, String>>> createUser(
#RequestPart("file") MultipartFile file, #RequestPart(required=false) User user) {
// ... CODE ...
}
However make sure your request looks something like this:
POST /createUser
Content-Type: multipart/mixed; boundary=B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
Content-Disposition: form-data; name="user";
Content-Type: application/xml; charset=UTF-8
<user><!-- your user xml --></user>
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E
Content-Disposition: form-data; name="file"; filename="A551A700-46D4-470A-86E7-52AD2B445847.dat"
Content-Type: application/octet-stream
/// FILE DATA
--B0EC8D07-EBF1-4EA7-966C-E492A9F2C36E--
You can use #RequestPart from
org.springframework.web.bind.annotation.RequestPart;
It is used as Combining #RequestBody and file upload.
Using #RequestParam like this
#RequestParam("file") MultipartFile file
you can upload only file and multiple single data (key value )
like
#RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public void saveFile(
#RequestParam("userid") String userid,
#RequestParam("file") MultipartFile file) {
}
you can post JSON Object data and and File both using #RequestPart like
#RequestMapping(value = "/patientp", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> insertPatientInfo(
#RequestPart PatientInfoDTO patientInfoDTO,
#RequestPart("file") MultipartFile file) {
}
You are not limited to using multipart file uploads directly as controller method parameters. Your form objects can contain Part or MultipartFile fields, and Spring knows automatically that it must obtain the values from file parts and converts the values appropriately.
Above method can respond to the previously demonstrated multipart request containing a single file. This works because Spring has a built-in HTTP message converter that recognizes file parts. In addition to the javax.servlet.http.Part type, you can also convert file uploads to org.springframework.web.multipart.MultipartFile. If the file field permits multiple file uploads, as demonstrated in the second multipart request, simply use an array or Collection of Parts or MultipartFiles.
#RequestMapping(value = "/patientp", method = RequestMethod.POST, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<?> insertPatientInfo(
#RequestPart PatientInfoDTO patientInfoDTO,
#RequestPart("files") List<MultipartFile> files) {
}
Happy To Help...
I have managed to solve problem:
#SuppressWarnings("rawtypes")
#RequestMapping(value = "/DataTransfer", method = RequestMethod.POST, produces = {
MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_UTF8_VALUE} )
#ApiOperation(value = "Sbm Data Transfer Service", response = Iterable.class)
#ApiResponses(value = { #ApiResponse(code = 200, message = "Successfully find."),
#ApiResponse(code = 400, message = "There has been an error."),
#ApiResponse(code = 401, message = "You are not authorized to save the resource"),
#ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
#ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
ResponseEntity processDataTransfer(#RequestPart(name="file") MultipartFile file, #RequestPart(name="param") DataTransferInputDto param);
have you tried
ResponseEntity<List<Map<String, String>>> createUser(
#RequestPart("file") MultipartFile file, #RequestBody(required=false) User user) {
or
ResponseEntity<List<Map<String, String>>> createUser(
#RequestPart("file") MultipartFile file, #RequestParam(required=false) User user) {
If this does not work can you show us mapping.xml

Resources