Spring DateTimeFormat.ISO.Date preconfigured pattern returns syntactically incorrect - spring

I am trying to pass a date in my URL in the format: 2014/12/12
So my url looks like:
http://192.0.0.1/api/getdata?date=2014-12-12
My controller method looks like:
public Iterable<Object> getObject(
#RequestParam(value = "date", required = false)
#DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date)
I am getting the tomcat error
<b>description</b>
<u>The request sent by the client was syntactically incorrect.</u>
I thought if I was using a pre-configured date pattern, there would no be an issue with parsing the date?
The request sent by the client was syntactically incorrect using #DateTimeFormat
I have looked at the question above which is similar. But the question is using MM/yy which isn't preconfigured. Do I still need a custom date deserializer anyway?

Spring supports rest URL, so your the date format you want to pass will be divided into 3 parts.
It will not be possible to send the date in url using this format.You need to change the date format which you want to pass.

The format yyyy/MM/dd is a bad format to be passing the date, for the reason Sambhunath mentioned. However if you have to use this format, you should register your binder
#InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
and remove the #DateTimeFormat(iso = DateTimeFormat.ISO.DATE) annotation, from the argument.
Another thing is that from your question is not clear if you want to use the yyyy-MM-dd format, in which case your code is fine, and the issue your having is the error in your request. Remove the & from
http://192.0.0.1/api/getdata?&date=2014-12-12
so make it
http://192.0.0.1/api/getdata?date=2014-12-12

Related

After upgrading to Spring Boot 2.1.8.RELEASE from 1.5.x, QueryParams for HTTP Get requests with + sign are getting encoded to spaces now

Before the upgrade, we had a #RestController controller end point with HTTP GET:
#RequestMapping(value = "/{dashboardDefinitionId}/widgets/{widgetId}", method = { RequestMethod.GET })
public ResponseEntity<JsonSerializer> getWidgetReportData(#PathVariable String dashboardDefinitionId,
#PathVariable String widgetId, ReportFilter reportFilter) { ... }
The ReportFilter has a startDate and endDate variables that used to be deserialized fine:
private Date startDate;
private Date endDate;
The UI was passing date strings with + signs encoding (%2b) in it for timezone information.
prior to the upgrade, the application was deserializing the strings fine into Date instances. It was encoding the %2b to '+' by the time it got to the controller and worked fine. Now, after the upgrade, it's failing bc it's seeing a space ' ' in the date instead of the + with additional timezone info in the date string.
My main question is, why is spring decoding query params, and then reencoding them somewhere downstream incorrectly and translating + in query params to spaces.
This would be an issue anywhere when then the UI is creating HTTP gets with query params with + in it, such as email addresses.

Read Full Query parameter String in Spring Boot

Is there a way for me to read the entire query string in the GET API? Since there can be a variable number of parameters I am already looking at using this
public void createUser(#RequestParam(required=false) Map<String,String> qparams) {
}
But I want to read the entire query string as well.
The reason being one of the parameters here is an HMAC which is calculated on the entire string. and we are using that HMAC for cross verification.
We have deep integration with third-party software. The issue here is that the third-party software can make a change to their API at any point in time.
Here's how you can do it.
#GetMapping("/test1")
void endpoint1(HttpServletRequest req) {
var qs = req.getQueryString() //returns the entire string
qs.split("&") //split to get the individual parameters
}

Sending multipart form data does not appear to work correctly

I'm trying to call an endpoint that accepts PUT requests and expects to be passed 3 different MultipartFile paramters. Let's call them A, B and C.
When I make a request to the same enpoint from Postman it works as intended. When I do it via the the reactor-netty lib I get back Error 400 Bad Request:
"Required request part 'A' is not present"
HttpClient
.create()
// skipping baseUrl and headers headers
.put()
.uri(ENDPOINT_URI)
.sendForm((req, form) -> form
.multipart(true)
.file("A", FILE_A, "application/json)
.file("B", FILE_B, "application/json)
.file("C", FILE_C, "application/json))
.response()
I could not find much info online to establish if this is the best way to achieve what I need. Can you please point me to where I'm going wrong or perhaps towards an alternative solution?
Thanks
After looking throught the source of the HttpClientForm (the class in which .file is called) I found this:
default HttpClientForm file(String name, InputStream stream, #Nullable String contentType) {
return file(name, "", stream, contentType);
}
as well as this:
default HttpClientForm file(String name, File file, #Nullable String contentType) {
return file(name, file.getName(), file, contentType);
}
Somehow I thought that the first paramter 'name' is the one that is matched with the #RequestParam value. By the looks of it its actually the second.
Also if using an input stream instead of a File I had to call the the file method with 4 paramters and pass the name explicitly as the second parameter like so:
file(name, "A", stream, contentType)

How to pass Bean shell processor variable in to HTTP Request body data in jmeter

I need to pass the Date format variable data from Bean shell processor to http request body
Below is my code and json where I passed variable data but it is not working
import java.text.SimpleDateFormat;
import java.util.Date;
Date enrolmentDate = new Date();
enrolmentDate.setDate(enrolmentDate.getDate());//+ ${__Random(1,50,)});
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
String formattedDate = df.format(enrolmentDate);
vars.put("StartDate",formattedDate);
log.info("########################"+formattedDate);
Below is the Http Request Body data
{
"articleId":""${ArticleId}",
"startDate":"${formattedDate}",
"endDate":"${Carttodates}"
}
When i run it Start date and end date is shown as ${formattedDate}, what will be the solution?
and in my JSON body data i want to send Start and End Date like "27/05/2019 14:34 "
Below is the Request I got
PUT data:
{
"articleId":"7694b207-936b-40b9-9c80-4b8097e67da1",
"startDate":"${formattedDate}",
"endDate":"${Carttodates}"
}
Change your request body to
{
"articleId":""${ArticleId}",
"startDate":"${StartDate}",
"endDate":"${Carttodates}"
}
The reason why this is required is because you are storing the date in "StartDate" variable in beanshell. Hence, you should use "StartDate" to access the value later in HTTP.
The other option is to store the value in "formattedDate" variable in beanshell and then you do not need to change it in HTTP request body.
You need to put formattedDate as the variable name also:
vars.put("formattedDate", formattedDate);

Send boolean to Parse

In Parse I have a class that has a string and a boolean column.
I want to update the object with new values and I do this:
RestSharp client =...
RestRequest request = new RestRequest(Method.PUT);
request.AddParameter("String column", "new string");
request.AddJsonBody("{"+"Boolean column"+":False\"}");
request.RequestFormat = DataFormat.Json;
var requestHandler = client.ExecuteAsync(...)
The response's status is OK and when I check with Parse dashboard on the server, the first column has changed. But the second column, the boolean one has not changed.
How should I set the boolean parameter?
I tried:
request.AddJsonBody("{"+"Boolean column"+":false\"}");
request.AddJsonBody("{"+"Boolean column"+":0\"}");
but none of them changed the value on the backend server.
When I tried:
request.AddParameter("Boolean column": false);
I got this error:
"{\"code\":111,\"error\":\"schema mismatch for ...; expected Boolean but got String\"}"
How can I fix this problem?
Thank you
check your mongodb collection 'SCHEMA' for the answer
Because the Parse will create a restricted condition according to the first record inserted for each collection.

Resources