在一个部分的同一个请求中,我得到NullPointerException,而在代码的其他部分则没问题。
@Controller
@RequestMapping(value="/event")
public class EventController {
@Autowired
EventValidator eventValidator;
@Autowired
private EventService eventService;
@InitBinder
private void initBinder(WebDataBinder binder) {
System.out.println(eventValidator.toString()); <<—— NULL HERE
binder.setValidator(eventValidator);
}
@RequestMapping(value="/add_event",method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<AjaxJSONResponse> postAddEventForm(@RequestPart("event") Event event, MultipartHttpServletRequest request,BindingResult result) throws MethodArgumentNotValidException, NoSuchMethodException
{
eventValidator.validate(event, result); <<——- OK HERE
//Check validation errors
if (result.hasErrors()) {
throw new MethodArgumentNotValidException(
new MethodParameter(this.getClass().getDeclaredMethod("postAddEventForm", Event.class, MultipartHttpServletRequest.class, BindingResult.class), 0),
result);
}
Boolean inserted = eventService.addEvent(event);
String contextPath = request.getContextPath();
String redirectURL = StringUtils.isEmpty(contextPath)?"/event":contextPath+"/event";
return new ResponseEntity<AjaxJSONResponse>(new AjaxJSONResponse(inserted,"Event Added Successfully",redirectURL), HttpStatus.OK);
}
}
在同一请求中,initBinder()
函数eventValidator
为NULL,而postAddEventForm()
函数eventValidator
正在保存实例。
请帮忙。
答案 0 :(得分:0)
initBinder()
方法的访问说明符应为public
-
public void initBinder(WebDataBinder binder) {