繁体   English   中英

如何解决通过 Z03D476861AFD3854510 测试 spring 引导 rest api 时遇到的错误?

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

我创建了一个简单的 api,它使用 spring 引导计算生日的剩余天数。 Tomcat服务器成功站起来,不报错。 但是,当我尝试通过 postman 发布它来测试 api 时,我在下面的屏幕截图中看到了错误。 我在下面分享了服务层的代码和controller层的代码。 我应该怎么做才能修复这个错误?

项目结构:

在此处输入图像描述

Spring 启动服务层:

@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 启动 controller 层:

@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 测试错误:

在此处输入图像描述

你在你的身体中添加你的参数,你不应该,它应该在 url 中:

代替:

http://localhost:8080/findBirthDay/

尝试:

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

在这里您可以找到有关如何使用请求参数的好教程:

使用请求参数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM