how to pass a parameter in the Header with multipartFormData - spring

#PostMapping(value = "/uploadCV" , consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> uploadCV(#RequestHeader("token") String token,
#RequestParam("file") MultipartFile cvFile) throws Exception {
log.info(token + cvFile.getOriginalFilename()));
return curriculumService.addCurriculum(token, pdfFile);
}
If i use MediaType.MULTIPART_FORM_DATA_VALUE don't get the value "token" :
2022-11-11 16:13:28.040 WARN 7283 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'token' for method parameter type String is not present]
If i use MediaType.MULTIPART_MIXED_VALUE don't get file:
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
how can i do?

The code that you have written with MediaType.MULTIPART_FORM_DATA_VALUE is absolutely correct. It is very clear from error message what is the issue.
2022-11-11 16:13:28.040 WARN 7283 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'token' for method parameter type String is not present]
Issue:
Code is expecting header name with key token should present in the request but it is not present.
Solution:
Please add header name with key token like this and try again.

Related

Spring-boot Api endpoint for uploading file not working after adding 'spring-boot-starter-hateoas' dependency

I have a simple API function to upload a file similar to:
#PostMapping(value = "/documents",
consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public Mono<ResponseEntity<String>> uploadDocument(#RequestPart Mono<FilePart> file){
return storeDocumentService
.upload(file)
.map(fileLocation->ResponseEntity.ok(fileLocation))
}
The code works ok and uploads the file. The problem comes when I want to make the response a bit better by returning the link to the uploaded file. For this I want to use HATEOAS 'org.springframework.boot:spring-boot-starter-hateoas'. As soon as I add the dependency 'org.springframework.boot:spring-boot-starter-hateoas' to my 'build.gradle' the endpoint stops working and I get a response:
{
"timestamp": "2023-02-20T04:28:10.620+00:00",
"status": 415,
"error": "Unsupported Media Type",
"path": "/documents"
}
and also I get in the logs:
2023-02-20T05:28:10.618+01:00 WARN 2993 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/pdf' is not supported]
It is important to point out that I upload a ".pdf" file with a header "Content-Type:multipart/form-data". And most important the only change in the working code and not working code is that i just add the dependency for HATEOAS 'org.springframework.boot:spring-boot-starter-hateoas'
For Uploading File We can easily use the type MultiPartFile , This will handles all the types of files and we can easily retrive the fileInputStream(data) from it.
The following code may helps you!..
#PostMapping("uploadExcelData")
public ResponseEntity<?> uploadExcelData(#RequestParam MultipartFile file) throws IOException {
List<...> dataList = fileHandling.convertFileAsJson(file);
if (!dataList.isEmpty()) {
return ....
} else {
return ResponseEntity.ok("No Records found !!");
}
}
I hope the above code will helps you to handle the File in the Endpoint.

Spring WebFlux - WebGraphQlInterceptor has empty request data

I would need to retrieve data from query headers for use in the GraphQl query controller. I searched around a bit and found the WebGraphQlInterceptor, but realized that when the WebGraphQlRequest argument is invoked it is completely devoid of information.
For example, this code:
#Component
class WebRequestInterceptor : WebGraphQlInterceptor {
val logger: Logger by lazy { LoggerFactory.getLogger(WebRequestInterceptor::class.java) }
override fun intercept(request: WebGraphQlRequest, chain: WebGraphQlInterceptor.Chain): Mono<WebGraphQlResponse> {
logger.info("URI", request.uri.toString())
logger.info("HEADERS", request.headers.toString())
logger.info("DOCUMENT", request.document)
return chain.next(request)
}
}
Returns:
2022-09-12 10:57:11.747 INFO 1 --- [or-http-epoll-1] b.s.b.gateway.auth.WebRequestInterceptor : URI *empty string*
2022-09-12 10:57:11.748 INFO 1 --- [or-http-epoll-1] b.s.b.gateway.auth.WebRequestInterceptor : HEADERS *empty string*
2022-09-12 10:57:11.748 INFO 1 --- [or-http-epoll-1] b.s.b.gateway.auth.WebRequestInterceptor : DOCUMENT *empty string*
P.S. The same thing happens if I try to log a single headers element using the request.headers.getFirst(..) function.

Spring Boot : Cannot replace Jackson with Gson , jackson still appears in logs

Gson dependency
implementation("com.google.code.gson:gson:2.9.0")
application-dev.properties
spring.http.converters.preferred-json-mapper=gson
# Format to use when serializing Date objects.
spring.gson.date-format="yyyy-MM-dd HH:mm:ss"
Function to handle incoming post request with data payload
#RequestMapping(path = [ControllerEndPoints.AddCheckingPoint], method = [RequestMethod.POST])
fun addCheckingPoint(#RequestBody reqData: ChartServerVo): ResponseEntity<Ack> {
var ok = true
val data = CheckingPointEntity()
val cpDto = reqData.checkingPointDto
val gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()
val payload = gson.toJson(reqData)
mLog("json: \n json $payload")
//val gDto = reqData.gDto
data.apply {
name = cpDto.name
description = cpDto.description
cpTypeId = cpDto.cpTypeId
isCumulative = cpDto.isCumulative
cpCategoryGroupId = cpDto.cpCategoryGroupId
isTemplate = cpDto.isTemplate
isTopTemplate = cpDto.isTopTemplate
}
cpr.save(data)
pcRepo.save(reqData.purusharthChartDto.toEntity())
pcMappingrepo.save(reqData.purusharthChartCpMappingDto.toEntity())
return ResponseEntity(Ack(ok), HttpStatus.ACCEPTED)
}
JSON Payload
{"checkingPointDto":{"cpCategoryGroupId":1641785600780,"cpTypeId":1,"description":"","isCumulative":false,"isTemplate":false,"isTopTemplate":false,"name":"asdf","dbCreateDts":"2022-03-05 11:54:01","dbCreateSource":"","dbUpdateDts":"2022-03-05 11:54:01","dbUpdateSource":"","id":0},"purusharthChartCpMappingDto":{"cpId":0,"id":0,"purusharthChartId":1647652877927},"purusharthChartDto":{"adminID":0,"description":"","endDate":"2022-12-30 11:54:01","id":1647652877927,"isSelfChart":false,"name":"asdf","startDate":"2022-03-05 11:54:01","userId":8}}
Error log
POST "/api/v1/cp-add", parameters={}
2022-03-05 16:52:32.118 DEBUG 1360 --- [nio-9000-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to in_.co.innerpeacetech.bkp.controller.CheckingPointController#addCheckingPoint(ChartServerVo)
2022-03-05 16:52:32.358 DEBUG 1360 --- [nio-9000-exec-2] o.s.web.method.HandlerMethod : Could not resolve parameter [0] in public org.springframework.http.ResponseEntity<in_.co.innerpeacetech.bkp.dto.Ack> in_.co.innerpeacetech.bkp.controller.CheckingPointController.addCheckingPoint(in_.co.innerpeacetech.bkp.dto.ChartServerVo): JSON parse error: Cannot construct instance of `in_.co.innerpeacetech.bkp.dto.ChartServerVo`, problem: `java.lang.IllegalArgumentException`; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `in_.co.innerpeacetech.bkp.dto.ChartServerVo`, problem: `java.lang.IllegalArgumentException`
at [Source: (PushbackInputStream); line: 1, column: 2]
2022-03-05 16:52:32.365 WARN 1360 --- [nio-9000-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `in_.co.innerpeacetech.bkp.dto.ChartServerVo`,
My data class variable from the error log
#JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
var dbCreateDts: Date = Date(),
Why is com.fasterxml.jackson.databind still present in the logs and why is the params empty in the logs POST "/api/v1/cp-add", parameters={}, I am testing the api from postman and the headers and everything is fine and the json is also properly formatted. I am new to spring, what am I missing.

SpringBoot 2.6.2 and form data

I am experimenting with a controller endpoint that looks like this:
#PostMapping("login")
fun login(
#RequestParam username: String,
#RequestParam password: String): ResponseEntity<LoginResponse> {
// ...
}
The request is send from a HTML form looking like this:
<form action="../api/login" method="POST">
<input id="username" type="text" placeholder="Enter Username" name="username" required=""><br>
<input id="password" type="password" placeholder="Enter Password" name="password" required=""><br>
<button type="submit">Login</button>
</form>
This works perfectly will with spring boot version 2.6.1. But after an upgrade to version 2.6.2 and adding spring cloud gateway it all of a sudden does not work any longer.
The log would look like this:
2022-01-11 14:33:09,618 [reactor-http-nio-2] DEBUG o.s.web.method.HandlerMethod - [3d97dc1a-1, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:13027] Could not resolve parameter [0] in public org.springframework.http.ResponseEntity<com.example.models.LoginResponse> com.example.login(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String): 400 BAD_REQUEST "Required String parameter 'username' is not present"
2022-01-11 14:33:09,656 [reactor-http-nio-2] DEBUG org.springframework.web.HttpLogging - [3d97dc1a-1, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:13027] Resolved [ServerWebInputException: 400 BAD_REQUEST "Required String parameter 'username' is not present"] for HTTP POST /api/login
I tried various things like:
#PostMapping(value = ["login"], consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE])
fun login(
#RequestParam paramMap: MultiValueMap<String,String>
): ResponseEntity<LoginResponse> {
//...
}
But also this fails with the following log:
2022-01-11 14:10:11,589 [reactor-http-nio-2] DEBUG o.s.w.s.a.HttpWebHandlerAdapter - [656327b1-1, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:11772] HTTP POST "/api/login"
2022-01-11 14:10:11,601 [reactor-http-nio-2] DEBUG o.s.w.r.r.m.a.RequestMappingHandlerMapping - [656327b1-1, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:11772] Mapped to com.example.ApiController#login(MultiValueMap)
2022-01-11 14:10:12,945 [reactor-http-nio-2] DEBUG o.s.w.r.r.m.a.RequestBodyMethodArgumentResolver - Form data is accessed via ServerWebExchange.getFormData() in WebFlux.
2022-01-11 14:10:21,640 [reactor-http-nio-2] DEBUG o.s.web.method.HandlerMethod - [656327b1-1, L:/0:0:0:0:0:0:0:1:8080 - R:/0:0:0:0:0:0:0:1:11772] Could not resolve parameter [0] in public org.springframework.http.ResponseEntity<com.example.models.LoginResponse> com.example.ApiController.login(org.springframework.util.MultiValueMap<java.lang.String, java.lang.String>): 415 UNSUPPORTED_MEDIA_TYPE
I would guess the error message with 415 UNSUPPORTED_MEDIA_TYPE is just misleading and it somehow fails to map the form-data. What can I do to get the API again accept form-data?
Trying something like:
#PostMapping(value = ["login"], consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE])
fun login(#RequestParam paramMap: Map<String,String>): ResponseEntity<LoginCodeResponse> {
// ...
}
Actually get's called but paramMap is always empty.
What actually works is the following:
#RestController
#RequestMapping("api")
class HelloWorldController(){
#GetMapping("hello")
fun helloName(#RequestParam name: String): String {
return "Hello $name!"
}
}
So for a normal get request #RequestParam works as expected.
Update
I seems to boil down to the following. With spring-boot-starter-webflux it seems the #RequestParam for form-data does not work. This seem to be a known issue.
implementation("org.springframework.boot:spring-boot-starter-webflux")
With spring-boot-starter-web it #RequestParam for form-data works.
implementation("org.springframework.boot:spring-boot-starter-web")
But this starter is not compatible with spring cloud. Using both spring-boot-starter-web with setting spring.main.web-application-type=reactive makes spring cloud gateway start with spring-boot-starter-web but still #RequestParam for form-data not working.
To get from data with POST working with webflux I did the following using the nicely provided awaitFormData method:
#PostMapping("authorize", consumes = [MediaType.APPLICATION_FORM_URLENCODED_VALUE])
suspend fun login(exchange: ServerWebExchange): ResponseEntity<LoginResponse> {
val formData = exchange.awaitFormData()
val username = formData["username"]?.get(0)!!
val password = formData["password"]?.get(0)!!
That is just a sketch of the essentials of the solution. If you want to use this you should also add some proper error handling to check whether parameters are there otherwise you will just have an error 500 which does not tell a lot.

AbpApiExceptionFilterAttribute - A value is required but was not present in the request

I have a webapi controller like below:
[ResponseType(typeof(SampleDto))]
public IHttpActionResult GetSample(string name, string guid)
and the request take name and guid as query string like:
http://www.example.com/api/Controller1/GetSample?name=james&guid=
the guid is empty.
When I issue the request, there is an Error:
WARN 2018-09-29 07:04:21,361 [18 ] nHandling.AbpApiExceptionFilterAttribute - Method arguments are not valid! See ValidationErrors for details.
Abp.Runtime.Validation.AbpValidationException: Method arguments are not valid! See ValidationErrors for details.
WARN 2018-09-29 07:04:21,361 [18 ] nHandling.AbpApiExceptionFilterAttribute - There are 2 validation errors:
WARN 2018-09-29 07:04:21,361 [18 ] nHandling.AbpApiExceptionFilterAttribute - A value is required but was not present in the request. (guid.String)
WARN 2018-09-29 07:04:21,361 [18 ] nHandling.AbpApiExceptionFilterAttribute - A value is required but was not present in the request. (guid.String)
Where can I change the validation rule?

Resources