所以,我知道JEE6和JEE7支持方法参数验证,如this blog post中所示。我有工作代码使用内置的java验证之一,如@Min。但是,我无法使复杂的bean对象验证工作。例如,当我将@Valid注释放在方法参数上时,它什么都不做。即使bean参数中包含无效参数,也会执行我的方法。所以,问题是@Valid有效吗?或者,方法验证是否局限于javax.validation.constraints annotations之一?
这可能是wildfly 8(我使用的ejb容器)中的错误
更新: 根据评论请求将类定义添加到我的问题中。 1 EJB
//This is the EJB
@Stateless
public class DefaultItineraryService {
public Itinerary tryMakingItinerary(
@Valid Airport startingAirport,
@Valid Airport destinationAirport,
Point startingPoint,
Point endingPoint,
@NotNull Long id) throws NoFlightsFoundException {
}
}
//This is the client servlet
@WebServlet(urlPatterns = "/api/itinerary/propose")
public class ProposeDefaultItineraryServlet extends HttpServlet {
@Inject
private ParamExtractor paramExtractor;
@EJB
private DefaultItineraryService itineraryService;
@EJB
private IPlaceDao placeDao;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Point startingPoint = paramExtractor.extract("start", Point.class);
Point endingPoint = paramExtractor.extract("end", Point.class);
Airport startingAirport = placeDao.findNearestAirport(startingPoint);
Airport endAirport = placeDao.findNearestAirport(endingPoint);
Long id = paramExtractor.extract("id", Long.class);
Itinerary itinerary = null;
try {
itinerary = itineraryService.tryMakingItinerary(startingAirport, endingAirport, startingPoint, endingPoint, id);
} catch (SystemException | NotSupportedException | HeuristicRollbackException | HeuristicMixedException | RollbackException e) {
throw new ServletException(e);
}
request.setAttribute("result", itinerary);
}
}
谢谢!