All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S7183.html Maven / Gradle / Ivy

The newest version!

Why is this an issue?

Spring provides the @InitBinder annotation to initialize a WebDataBinder instance for controllers. This is useful to bind request parameters to a model object, and to plug converters and formatters into this process.

Methods annotated with @InitBinder must not have a return value, otherwise the controller containing them will throw an exception when invoked.

This rule raises an issue when a method annotated with @InitBinder does not have a void return type

How to fix it

Code examples

Noncompliant code example

@Controller
public class MyController {

	@InitBinder
	public String initBinder(WebDataBinder binder) { // Non compliant, make the @InitBinder method return void
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        return "OK";
	}

	// ...
}

Compliant solution

@Controller
public class MyController {

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	// ...
}

Resources

Documentation





© 2015 - 2025 Weber Informatics LLC | Privacy Policy