okomut
okomut

Reputation: 47

How can I solve the error I get while testing the spring boot rest api via postman?

I created a simple api that calculates the days left for the birthday using spring boot. Tomcat server successfully stands up, does not give an error. But when I try to test the api by posting it via postman, I get the error in the screenshot below. I shared the codes in the service layer and the codes in the controller layer below. What should I do to fix this error?

Project Structure:

enter image description here

Spring boot service layer:

@Service
public class BdayService {
    private int yearOfBirth;
    private int monthOfBirth;
    private int dayOfBirth;

    public String setTime(LocalDate birthday) {
        if(birthday == null){
            return null;
        }

        final boolean isToday = birthday.getMonth() == LocalDate.now().getMonth()
                && birthday.getDayOfMonth() ==LocalDate.now().getDayOfMonth();

        if(birthday.isAfter(LocalDate.now())) {
            return "Please insert a valid birthday.";
        } else if (isToday) {
            return "HAPPY BIRTHDAY!";
        } else {
            yearOfBirth = birthday.getYear();
            monthOfBirth = birthday.getMonthValue();
            dayOfBirth = birthday.getDayOfYear();
            return timeRemaining(monthOfBirth, dayOfBirth);
        }
    }

    private String timeRemaining(int month, int day) {
        int currYear = LocalDate.now().getYear();
        int currMonth = LocalDate.now().getMonthValue();
        int currDay = LocalDate.now().getDayOfYear();

        //TIME REMAINING
        int hrsRemaining = 24 - LocalTime.now().getHour() - 1;
        int minsRemaining = 60 - LocalTime.now().getMinute() - 1;
        int secsRemaining = 60 - LocalTime.now().getSecond();
        //Find how many days in the year in order to calculate the number of days left
        final int daysInYear = LocalDate.now().isLeapYear() ? 366 : 365;
        int daysRemaining = dayOfBirth > currDay ?
                dayOfBirth - currDay - 1:
                (daysInYear - currDay) + dayOfBirth - 1;

        final int age = monthOfBirth > currMonth ?
                currYear - yearOfBirth :
                currYear + 1 - yearOfBirth;

        return ("There are " + daysRemaining + " days,\n"
                + hrsRemaining + " hours,\n"
                + minsRemaining + " minutes and\n"
                + secsRemaining + " seconds until\n"
                + "you turn " + age + "!");
    }
}

Spring boot controller layer:

@Controller
public class BdayController {

    @Autowired
    private BdayService bdayService;

    @PostMapping("/findBirthDay")
    public String findBirthDay(@RequestParam("bday") String bday){
        //bday format => dd-mm-yyyy
        int year = 0;
        int month = 0;
        int day = 0;

        if(bday.contains("-")){
            String[] bdayArray = bday.split("-");

            year = Integer.parseInt(bdayArray[2]);
            month = Integer.parseInt(bdayArray[1]);
            day = Integer.parseInt(bdayArray[0]);

            return bdayService.setTime(LocalDate.of(year, month, day));
        }else{
            return "Given date format is not invalid!";
        }
    }
}

Postman api test error:

enter image description here

Upvotes: 0

Views: 222

Answers (1)

Stultuske
Stultuske

Reputation: 9437

You are adding your parameter in your body, you shouldn't, it should be in the url:

Instead of:

http://localhost:8080/findBirthDay/

Try:

http://localhost:8080/findBirthDay?bday="02-03-1995"

Here you can find a good tutorial on how to use Request Parameters:

Use Request Parameters

Upvotes: 1

Related Questions