How do I configure a controller bean? - spring

If we have a look at the Mkyong tutorial for a simple Hello World MVC application.
Now lets say instead of returning a straight hello, the controller instead calls a business logic layer to get what it wants.
#Controller
#RequestMapping("/welcome")
public class HelloController {
private BusinessLogic myBusinessLogic;
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
String text = myBusinessLogic.getMessage();
model.addAttribute("message", text);
return "hello";
}
}
I have two questions here.
How do I instantiate myBusinessLogic? In the exampke mkyong has given, there's no bean configuring the HelloController.
How do I configure the bean's scope?

Related

How to call Spring Boot RESt api using Spring MVC?

I have created REST api using Spring Boot.
So, that is the fragment of it:
#RestController
#RequestMapping("/api/employee")
public class EmployeeController {
#Autowired
private EmployeeService employeeService;
#GetMapping(value = "/all", produces = "application/json")
public ResponseEntity<List<Employee>> getAllEmployees() {
return ResponseEntity.ok(employeeService.findall());
}
Now, I would like to create more like MVC application part - simple view that shows all Employees using thymeleaf. (Just simple UI to use this app more convinient than sending curl requests)
#Controller
public class MainPageController {
#GetMapping("/employees")
public String showEmployees() {
// i don't know what to do here
return "employeesPage";
}
What is the appropriate way to do so? Is there a more simple way to do it?
Looking forward for your answers!
So you do exactly the same you did on your EmployeeController but instead of a JSON, you return a view.
Get your employes through EmployeeService and put them on a collection
Create your employee view under /templates folder (many tutorials of how to do it)
return this view with your collection of employees
Example:
#GetMapping(value = "employees")
public ModelAndView showEmployees() {
ModelAndView mav = new ModelAndView("employeesPage");
mav.addObject("employees", employeeService.findall());
return mav;
}
Check here for more detailed info:
https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

calling a method by class level annotated with #RequestMapping that includes an autowired class

I am trying to call a method that is annotated with #RequestMapping(signIn) through a class level (from method: authentication) like so:
#RequestMapping(value = /authenticate, method = RequestMethod.POST)
public #ResponseBody Response authentication(HttpServletRequest request)
{
UserController user = new UserController();
return user.signIn(request, null);
}
and my controller looks like:
#Autowired
private UserManager userManager;
#RequestMapping(value = /signin, method = RequestMethod.POST)
public #ResponseBody Response signIn(HttpServletRequest request) {
JsonObject json = Misc.parseJson(request);
String lang = Misc.getLang(request);
user.setEmail(Misc.getEmail(json));
user.setPassword(Misc.getEncryptedPassword(json));
return ResponseUtils.success(userManager.auth(user, lang));
}
user manager is annotated with #component:
#Component
public class UserManager {
public User auth(User user, String lang) {
....
return user;
}
}
Problem is when I call the method "signIn" and just new-up a UserController instance through "/authenticate" mapping, the UserManager becomes NULL. So now I'm assuming that autowiring doesn't work when it's done this way.
Is there any other way to call the signIn method? I would hate to copy paste an already existing code to another class just to get this to work...
Autowiering only works in spring managed bean. If you create a class with new keyword, it is not a spring managed bean and autowiering would not work.
You can try to autowire the class which contains the method which is annotated or better put the code in a service class which can be used by both methods.
It's not problem with #Autowired .There are two type of Annotation
firstly method base annotation and field level annotation. You just used field level annotation.Check your import class with "org.springframework.beans.factory.annotation.Autowired" or it can be problem with initiation of "UserManager"
I don't know why you not moving logic into separate Service classs, but try this:
UserController.java
public UserController(UserManager userManager) {
this.userManager = userManager;
}
and then inside controller where authentication resource method is located:
#Autowired UserManager userManager;
#RequestMapping(value = /authenticate, method = RequestMethod.POST)
public #ResponseBody Response authentication(HttpServletRequest request) {
UserController user = new UserController(userManager);
return user.signIn(request);
}
So in the end I just separated the logic instead. Though one solution that I tried and I could have used was to just add another mapping to the signIn method instead of adding a new method in the other class since the logic was similar. Still I opted for a separate logic instead since there were a lot of unnecessary code in the signIn method for my purpose.

Handling Session in spring MVC?

i'm actually new to handling with spring mvc and in particular i have to implement session in spring mvc. Can anybody guide me out of how to handle with session in spring mvc ?
It is the example of controller method level session
#Controller
Class YourController{
#RequestMapping(value = "/yourrequest", method = RequestMethod.GET)
public String yourMethod(Model model,HttpSession session){
return null;
}
}
You can also put session valid to Controller Class.It is just done by adding SessionAttribute.This will be available in all methods in YourController Class
#Controller
#SessionAttribute("sessionName")
Class YourController{
#RequestMapping(value = "/yourrequest", method = RequestMethod.GET)
public String yourMethod(Model model){
model.addAttribute("sessionName",valueToBeAddedInSession);
return null;
}
}
Here the name of object added in Session should be same as it is added in modelAttribute e.g. sessionName.
The problem in case of session attribute is that we have to invalidate it manually
Thank you.
#Controller
#SessionAttributes({ "sessionVar1","sessionVar2" })
public class StartController {
public ModelAndView methodName(){
ModelAndView modelandView = new ModelAndView("view");
modelandView.addObject("sessionVar1", "sessionVal1");
modelandView.addObject("sessionVar2", "sessionVal2");
return modelandView;
}
}

How to Generify REST Controller in Spring MVC to Remove Duplication

I have a design issue I cannot for the life of me figure out.
Good code has no duplication. I have generified my DAO so all basic crud operations are inherited
I'm trying to do the same with Spring MVC with Annotated Controllers.
I found this question but no answer is there: How to Remove Duplication from Spring 3 MVC Standard and Ajax Request Controllers and Views
If I have something like the below example, assuming I refactor to use AbstractBaseService and BaseModel (I did this but don't have the code), how can I put the annotation info into something like a GenericAbstractBaseController or BaseController interface? I've tried (don't have the code here) but the problem is that Annotations ARE NOT inherited and CANNOT be added at run time.
I see that javassist can be used for bytecode modification so that I actually CAN add annotations after compiling to keep the code clean but this appears to be overly complex.
I sent a note to a mentor and he suggested using AOP with naming convention to weave advice or reflection to identify the annotations from the inherited class but I'm not certain how I could actually give this info to spring given that I cannot actually add annotations at runtime. I think I'm missing some critical key here that someone will come and drop - he only sent me back a couple lines.
Here is the code - how can I remove the duplicate crud logic.
#Controller
#RequestMapping("/users")
public class UserController {
#Autowired
UserService userService;
#RequestMapping(value="/", method = RequestMethod.GET)
public #ResponseBody List<User> doGetIndex(ModelMap model) {
return userService.listPage(0, 10);
}
#RequestMapping(value="/{name}", method = RequestMethod.GET)
public #ResponseBody User doGet(#PathVariable String name, ModelMap model) {
return userService.getByUsername(name);
}
//post
//put
//delete
I don't quite understand what do you mean by "annotations are not inherited", but as far as I remember the following approach should work:
public class AbstractController<T> {
#RequestMapping(value="", method = RequestMethod.GET)
public #ResponseBody List<User> doGetIndex(ModelMap model) { ... }
#RequestMapping(value="{name}", method = RequestMethod.GET)
public #ResponseBody T doGet(#PathVariable String name, ModelMap model) { ... }
}
#Controller
#RequestMapping("/users")
public class UserController extends AbstractController<User> { ... }

#Autowired for #ModelAttribute

I'm very new to Spring and I'm encountering the following problem.
I've got the following Controller, in which the #Autowired works perfectly (tried debugging and it works fine).
#Controller
#RequestMapping(value = "/registration")
#SessionAttributes("rf")
public class RegistrationController
{
#Autowired
UserJpaDao userDao;
#RequestMapping(method = RequestMethod.GET)
#Transactional
public String setupForm(Model model) throws Exception
{
model.addAttribute("rf", new RegistrationForm());
return "registration";
}
#RequestMapping(method = RequestMethod.POST)
#Transactional
public String submitForm(#ModelAttribute("rf") RegistrationForm rf, Model model) throws Exception
{
// ...
User user = rf.getUser();
userDao.save(user);
// ...
return "registration";
}
}
But when I submit my form, the #Autowired field in my RegistrationForm remains null.
RegistrationForm.java:
#Component
public class RegistrationForm
{
#Autowired
CountryJpaDao countryDao;
// ... fields...
public RegistrationForm()
{
}
#Transactional
public User getUser() throws InvalidUserDataException
{
//...
Country c = countryDao.findByCode("GB"); // Throws java.lang.NullPointerException
// ...
}
// ... getters/setters...
}
Here is the form's HTML/JSTL:
<form:form method="POST" modelAttribute="rf">
...
</form:form>
Can anyone help me?
Thank you.
(inspired by this post on SpringSource forums)
You're mixing up your concepts here. You use the likes of #Component and #Autowired for Spring-managed beans, and #ModelAttribute for transient, throwaway objects that are used to bind form data. The two should not be mixed. Your #Component and #Autowired annotations on RegistrationForm will be ignored by Spring, because they're not appropriate in that context.
Classes like RegistrationForm should represent the form data, and nothing else. Typically, the controller would ask RegistrationForm for the user ID, and would then look at the actual User object from the DAO itself. If you want RegistrationForm to look up the User itself, then your controller needs to manually supply the DAO to RegistrationForm when it asks for the User object.
As far as that post on the Spring forum is concerned, you'll notice that it never received an answer. It's not a good source to take inspiration from.
Note that I'm not saying that desiring to autowire beans into a form back object is a bad idea, I'm just saying that Spring doesn't do that.
It would work if you use the #Configurable annotation on your model, and this aspectJ configuration on your gradle file:
compileJava << {
ant.taskdef(
resource: 'org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties',
classpath: configurations.compile.asPath)
ant.iajc(
inpath: sourceSets.main.output.classesDir.absolutePath,
classpath: configurations.compile.asPath,
aspectPath: configurations.aspects.asPath,
destDir: sourceSets.main.output.classesDir.absolutePath
)
}
In this way aspectJ will generate code that does the auto wiring.
#Configurable
public class RegistrationForm
{
...
}

Resources