Code is not update in Maven war - maven

I am working on a Spring MVC and Hibernate Project.When i build war clean install and deploy in tomact.In console old code is running Means like this is my index controller
#Controller
#RequestMapping({ "/index" })
public class IndexController {
private final Logger logger =
LogManager.getLogger(this.getClass().getSimpleName());
#RequestMapping(method = RequestMethod.GET)
public String index(ModelMap model, final Principal principal) {
//logger.debug("Enter in Get method IndexController");
return "index";
}
}
you can see i comment the logger but it print in console.So i think it comes from another controller then i delete all controllers from my code and after deploying it still print logger and i also remove logger from this controller and after deploying it still print logger.I dont know why my code is not update in war.Can anyone help me

Please clear local repository and then do clean install
For windows you can find .m2 repository in your C:\Users\{yourUsername}\.m2

Related

Changes in Application properties doesn't impact - Springboot

I am using Spring boot 2.5.4. I have written web application. Now i am facing issue with application.properties file variables. If i am changing existing value, In code old value is been read, newly defined object is not coming.
Find the below application.properties file
spring.datasource.url=jdbc:mysql://XXXXXXXXXXX:3306/test
spring.datasource.username=user
spring.datasource.password=XXXXXXXXXXXXXXXX
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# Config Variables
ml.url=https://google.com/ml/entities
main.url=https://xxxx.com/staging/mainfile
and application config java file
#Component
public class ApplicationConfig {
#Value("${ml.url}")
public String mlurl;
#Value("${main.url}")
public String mainurl;
#PostConstruct
public void initThat(){
that = this;
}
}
reading variable in code as
#RequestMapping("/readfile")
#RestController
public class AppointmentResource {
private static Logger S_LOGGER = LogManager.getLogger( AppointmentResource.class );
#Autowired
private ApplicationConfig applicationConfig;
#GetMapping(value = "/websiteUrl",produces = MediaType.APPLICATION_JSON_VALUE)
public String getProduct() {
String websiteUrl = applicationConfig.mlurl;
S_LOGGER.info("website url is " + websiteUrl);
return websiteUrl;
}
}
After compiling for few times. Then i changes ml.url to https://google.com/prod/entities/test
but in code still i am getting as https://google.com/ml/entities.
Can anyone help in getting latest changes from application.properties
I am struck here. Help
I fixed it. It was picking from the folder config. which i created for different environments

Tomcat+Spring boot redirect ignoring contextPath

I'am using Spring boot and Tomcat7 to build a vehicle management system.
The base path is localhost:8080/vehicle
My server setting:
server.contextPath=/vehicle
My IndexController:
#RequestMapping("/")
public class IndexController extends BaseController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
return "redirect:/login";
}
}
But when I go straight to get this view, my path is ...../login instead of ..../vehicle/login And so it returns 404 Error.
Also when I tried to use return "redirect:/vehicle/login"; it still goes to ...../login.
So what is wrong with my code. Why the server can't recognize the contextPath.
In application.properties use : server.servlet.context-path=/contextPath.
Example:
server.servlet.context-path=/vehicle
Tomcat Server:
In the webapps folder, the context path(vehicle) must be the same as the folder name(vehicle).

Dependency Injection with dynamically instanciated class with Spring Boot

I'm trying to develop a spring-boot application which offer the possibility for the user to create and call some simple workflows.
The steps of the workflows are already written (they all extends the same class), and, when the user create a workflow, he/she just pick which steps he wants to include in his it. The steps and the workflows are saved in a database.
My problem comes when the user call the workflow: I want to instanciate dynamically each step using the class loader but with the dependencies injected by spring!
Here is an example of a plug-in:
public class HelloWorldStepPlugin extends StepPlugin {
private static final Logger LOG = LogManager.getLogger();
#Autowired
private HelloWorldRepository repository;
public HelloWorldStepPlugin() {
super(HelloWorldStepPlugin.class.getSimpleName());
}
#Override
public void process() {
LOG.info("Hello world!");
this.repository.findAll(); // <= throw a NullPointerException because this.repository is null
}
}
Here is how I execute a Workflow (in another class):
ClassLoader cl = getClass().getClassLoader();
for (Step s : workflow.getSteps()) {
StepPlugin sp = (StepPlugin) cl.loadClass(STEP_PLUGIN_PACKAGE + s.getPlugin()).newInstance();
sp.process();
}
How can I do to have my HelloWorldRepository injected by Spring?
Is there a much better approach to do what I intend to?
I suggest you declare your steps as prototype beans. Instead of saving class names in the database, save bean names. Then get the steps and the plugins from the spring context (i.e. using getBean()).

Is there an equivalent of a "beforeHandler" for spring 4?

I have a controller with a requestmapping..
#Controller
public class TestController {
private static final String template = "Hello there, %s!";
private final AtomicLong counter = new AtomicLong();
#RequestMapping("/hello")
public #ResponseBody String hello() {
return "Hello";
}
}
How can I make it such that everytime a user goes to a RequestMapping, or whichever url, some other method is called to println to the console the URL the user is at before it actually enters the method "hello"?
I would like to use this method to ensure users have proper credentials in the future. I see there are #PreAuthorize annotation, but there doesnt seem to be a method associated with it that I can write my own logic, i.e. do a simple println to the console with the current URL the user is at.
You have a number of options.
With Spring, you can implement and register a HandlerInterceptor and implement its preHandle method to log the request URL, which you can reconstruct with the various HttpServletRequest methods.
With pure servlet-api, you can implement and register your own Filter which logs the request URL and then continues the chain, with doFilter(..).
You can also use AOP, advise all your #RequestMapping annotated methods with a #Before advice that logs the URL.

Unittesting MVC controller

Im trying to learn MVC combined with IoC and DI. In my project I also use Castle, Automapper, NHibernate, NHibernateIntegration.
Thanks to the excellent example "ToBeSeen" app by Kozmic I think I have the solution pretty much set up now. But now I want to unittest basic save operation at the controller method level. I just dont get how to go about this. I expect my problem is pretty simple but Im a novice at this and any help is appreciated.
I have a dossiercontroller like this:
[Authorize]
[Transactional]
public class DossierController : BaseController
{
private readonly IRepository repository;
private readonly IMappingEngine mappingEngine;
private readonly ILogger logger;
public DossierController(IRepository repository, IMappingEngine mappingEngine, ILogger logger)
{
this.repository = repository;
this.mappingEngine = mappingEngine;
this.logger = logger;
}
As you can see it needs a repostitory, mappingengine and logger. As I understand all this is configured and wired up at runtime.
Next I have a controller method that is used to save a dossier:
[HttpPost]
[ActionName("Dossier")]
[Transaction]
[AcceptVerbs(HttpVerbs.Post)]
[AcceptParameter(Name = "button", Value = "save")]
public ActionResult Dossier_Save(string button, DossierModel dossierModel, string returnUrl)
{
if (!Request.IsAuthenticated)
return RedirectToAction("Index", "Home");
if (!ModelState.IsValid) return View(dossierModel);
Dossier dossier = mappingEngine.Map<DossierModel, Dossier>(dossierModel);
repository.Save(dossier);
return View();
}
Here I just want to automap the dossiermodel into a dossier and save it using the repository.
Now I want to unit test this using Nunit. I dont want to mock anything, I want a real persistence test from the controller level. For this I have to create a new dossiercontroller and pass in the correct arguments. Now Im lost. How do I create and configure the arguments so they work exactly like in the web application. For instance: How do I create a correctly configured Automapper in a Unittest? How do I create a correctly configured repository? Should I use a container?
Any help, especcialy a code example is highly appreciated!
Thanks in advance.
If you don't want to mock anything, you will need to instantiate concrete versions of the services required by the constructor of your controller.
e.g.
IRepository repository = GetRepository();
IMappingEngine mappingEngine = GetMappingEngine();
ILogger logger = GetLogger();
DossierController controller = new DossierController(repository, mappingEngine, logger);
ActionResult rsult = controller.Save(...etc...);
As to how you are to configure and instantiate these, you will essentially need to do this in exactly the same way that your MVC app is doing this.
If you are unsure where in your MVC app these are being configured, I'd suggest that the first place to look is in the IoC configuration and work backword from there.

Resources