Tuesday 15 September 2015

5 most common and frequent problems you get when using Spring Framework

Hello fellow developers !! I am writing you and me this article, because as a web developer I use Spring framework a lot recently for college assignements or work projects, and I keep getting those 5 errors frequently. I also noticed I am not the only one who keeps getting them and forgetting how to fix them the next time they show up.
By the way, though they consume a lot of time if one doesn’t get used or learn how to suppress them, they are super easy to fix errors. So here is once and for all how to do it:

1) Port 4848 occupied

You need to go to the glassfish bin directory, mostly under C:\Program Files (x86)\glassfish\bin and run the file stopserv.bat. Start your glassfish via your IDE or just run your project and It will start automatically and make sure the project is deployed after starting if any other error shows up.
PS: I mostly use Intellij IDEA for JEE projects, and Glassfish server.

2)Wrong bean name and you don’t know it !!


For example in my recent project I needed a multipart resolver, and in my WebAppConfig.java, I named my bean commonsMultipartResolver because I thought that it is natural for me to call a CommonsMultipartResolver, “commonsMultipartResolver”, but it was wrong. Spring was looking for a resolver named “multipartResolver”, it took me one week to see it and I was no near realizing it if I didn’t send my code to be checked by another developer, because I am the one who wrote it and my mind is telling me that it is a Commons Multipart Resolver so even when I was checking for solutions I wasn’t paying attention for the naming.

3)Resource not found ! My favorite !


The one and only thing to do is to check the orthograph of the related resources: the returned view , the requested mapping value, the naming of the model or request attributes, URLs, etc…

4)What was the password again?!!


You keep forgetting the passwords to your different testing accounts. Create a default user (or what we also call a mock) to be able to verify them even if you get back on your project after a long periode of time.
For exampe when I am using spring security, I created a CustomUserDetailsService as follows:
@Service(“customUserDetailsService”)
public class CustomUserDetailsService implements UserDetailsService { 
public static final boolean test = true; @Autowired private UtilisateurServices clientBo; 
/** 
* {@inheritDoc } 
*/ 
@Override 
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException { 
Utilisateur domainUser; try { 
domainUser = test ? MockUtils.getAdmin() : clientBo.findByLogin(login); 
//TODO make sure to always use this instance to update user in order not get outdated data compared to DB return domainUser; 
} catch (UtilisateurNotFound ex) { Logger.getLogger(CustomUserDetailsService.class.getName()).log(Level.SEVERE, null, ex); throw new UsernameNotFoundException(login, ex); }
}
}
and the code for the Mock beeing:
public class MockUtils { public static Administrateur getAdmin() { Administrateur admin = new Administrateur(); admin.setRoles(“ROLE_ADMIN”); admin.setNom(“admin”); admin.setLogin(“admin”); admin.setPassword(PasswordEncoderUtil.getPasswordEncoder().encode(“password”)); return admin; }
}
If you need further explanation about this feel free to contact me.

5) Resource not found AGAIN!!

You probably forgot to map the new folder you just added in your WebAppConfig.java as follows:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“/css/**”).addResourceLocations(“/WEB-INF/views/css/”);
registry.addResourceHandler(“/js/**”).addResourceLocations(“/WEB-INF/views/js/”);
registry.addResourceHandler(“/imgs/**”).addResourceLocations(“/WEB-INF/views/imgs/”);
registry.addResourceHandler(“/fonts/**”).addResourceLocations(“/WEB-INF/views/fonts/”); }
PS: Make sure to copy and paste this in your Keep !

No comments

Post a Comment

© It's CHAYMA
Maira Gall