Springboot:PathVariable验证,其中条件{variable}.isBlank/{variable}.isEmpty不起作用

lstz6jyr  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(41)

我有一个控制器,我想验证PathVariable,它不能为null。例如,http://localhost:8080/api/details/pathred-timeoff/,我想验证此路径中“/”后面的路径变量。
我在这里有一个控制器,我想在这里抛出带有错误消息的Exception。

@RestController
@CrossOrigin("*")
@RequestMapping("/api/details")
public class DetailsController {
    @Autowired
    DetailsService detailsService;
    
    @Autowired
    OvertimeService overtimeService;
        
    // TESTING HERE!!!!
    @GetMapping("/expired-timeoff/{empNo}")
    public List<Details> getExpiredTimeOffBalancesByUser(@PathVariable String empNo) {

        if (empNo.isBlank() || empNo.isEmpty() || empNo.trim().equals("a")) {
            throw new InvalidArgumentException("Error Message");
        }
        return detailsService.findExpiredTimeOffBalancesByCreatedBy(empNo);
    }

}

字符串
同时,当我从“http://localhost:8080/api/details/datared-timeoff/a”获取时,我可以输入条件并捕获InvalidArgumentException消息Successfully catch my exception
当我从“http://localhost:8080/api/details/whitelabel-timeoff/"获取时,它只会返回默认的Whitelabel 404错误Exception not catched
我做了一些研究,它是说,路径变量不能为空,但我可以做什么,以提示InvalidArgumentException,如果用户不小心使路径变量“空”?
这是我的@RestControllerAdvise文件

package com.sicmsb.timeofftracker.controller.Advice;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;

import com.sicmsb.timeofftracker.exceptions.ResourceNotFoundException;
import com.sicmsb.timeofftracker.exceptions.InvalidArgumentException;

@RestControllerAdvice
public class RequestExceptionHandler {
    @ResponseStatus(HttpStatus.NOT_FOUND) //404
    @ExceptionHandler(ResourceNotFoundException.class)
    public String notFound(Exception e) {
        //e.printStackTrace();
        return e.getMessage();
    }
    
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //500
    @ExceptionHandler({RuntimeException.class, Exception.class})
    public String internalError(Exception e) {
        e.printStackTrace();
        return "error/internal_error";
    }
    
    @ResponseStatus(HttpStatus.BAD_REQUEST) //400
    @ExceptionHandler({InvalidArgumentException.class})
    public String badRequest(Exception e) {
        return e.getMessage();
    }
}


最新消息:看起来我可以像这样处理这个错误。有没有什么方法可以让我只使用一个函数来处理所有的错误?尝试了String.utils(empNo)和empNo==null,仍然没有进入条件。

// Handles cases where empNo is NOT provided
@GetMapping("/expired-timeoff/")
public List<Details> getExpiredTimeOffBalancesByUser() {
    throw new InvalidArgumentException("Error Message2");
}

pbwdgjma

pbwdgjma1#

你可以这样做:

@GetMapping({"/expired-timeoff/", "/expired-timeoff/{empNo}"})
 public List<Details> getExpiredTimeOffBalancesByUser(@PathVariable(required=false) String empNo) {
   // test if empNo is null
 }

字符串

juzqafwq

juzqafwq2#

尝试在IF语句中添加一个条件,以检查empNo是否为null。

if (empNo == null || empNo.isBlank() || empNo.isEmpty() || empNo.trim().equals("a"))
{
throw new InvalidArgumentException("Error Message");
}

字符串

相关问题