2

One of my endpoint takes 2 parameters in Java code with data/format type as Date. In swagger, the API shows expected input as:

string($date-time)

(query)

When I enter 2020-07-07T01:08:10Z in both the parameter boxes, I get status code 400 Bad request in response. I also tried with 2020-07-07T01:08:10.873Z.

Am I giving wrong Date format?. Please help.

Edit:

@GetMapping("/findAllEventsWithoutLogin")
    public Collection<GeneralEvent> getAllEvents(@RequestParam("start_date") Date startDate,
            @RequestParam("end_date") Date endDate) {

package com.events.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket productApi() {

        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }
}

enter image description here

Edit:

FYI: The same Date format is working fine when I am passing Date in body, but not when I am passing it as a query parameter. Below is working fine.

enter image description here

Meen639
  • 77
  • 1
  • 9
  • I'd guess that the Swagger is wrong - the code under it is expecting something different than Swagger documents. Your code input looks fine. Do you have access to the underlying service? – stdunbar Jul 07 '20 at 01:41

1 Answers1

3

No, You are not using the wrong date format.

  • date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2020-07-07T01:08:10Z

There might be some other thing that is making your request not reachable to the backend java server. Please Post your minimal java code and if possible then also swagger minimal yml definition. So, that we can check what is wrong that you are facing.

After asking various ping pong questions which clarify my doubt.

Not I find there is one issue with your backend code that is you are using start_date and end_date java.util.Date as a query param. But you are requesting with a string which throws parsing IllegalArgumentException and causing 400 Bad Request. So, to fix this you need to change your backend code to add format as @DateTimeFormat (iso = DateTimeFormat.ISO.DATE_TIME) which is illustrated perfectly on-site baeldung. Change code is given below:

@GetMapping("/findAllEventsWithoutLogin")
    public Collection<GeneralEvent> getAllEvents(@RequestParam("start_date")  @DateTimeFormat (iso = DateTimeFormat.ISO.DATE_TIME) Date startDate,
            @RequestParam("end_date")  @DateTimeFormat (iso = DateTimeFormat.ISO.DATE_TIME) Date endDate) {

Please check this link to know about this issue in detail.

If you want to check cooked code then visit my github.

Community
  • 1
  • 1
Ashish Karn
  • 1,127
  • 1
  • 9
  • 20
  • The screenshot and the code endpoint is different. searchEventsByCategoryWithoutLogin and findAllEventsWithoutLogin. Please check you are using the right endpoint while hitting rest service. – Ashish Karn Jul 07 '20 at 02:03
  • If there is any problem with start_date and end_date especially date format conversion then those values will be assigned as **null** but it will not respond with `400 Bad Request`. – Ashish Karn Jul 07 '20 at 02:08
  • Yes, by mistake had copied the wrong screenshot. But hitting the right end point in swagger, – Meen639 Jul 07 '20 at 02:13
  • There is one problem with your java backend code and that is you are sending start_date as String and receiving as java.util.Date. So, to fix this you can check the answer part that I am going to update accordingly. – Ashish Karn Jul 07 '20 at 04:50