HttpServelt Class flow - servlets

I am new to java and greatly appreciate help.
I see HttpServlet class is defined as abstract and after googling I understood ,it is to prevent object creation and not to execute HttpServlet Provided doXXX method ...
however if class A extends HttpServlet .. then objectofA.Service() would be called which is in HttpServlet and doXXX method of Class A would be executed even if its just extends ( calling which version of doxx() can happen even with out abstract) what is real reason to make it abstract am i missing something ? In web.xml if we are routing to ClassA then which class would be instantiated ?
I also wrote piece of code depcting HttpServercalls and doget() of CustomeSerlet is being called with out having ExampleHttpServlet class as abstract.
Im confused about flow from container to servlet now.
Output:
CustomServlet class loaded
CustomServlet class
In zero arg init() of ExampleHttpServelet class
In doget Method from CustomServlet class
tried all google searches but did not understand real reason

Related

Class without Verticle implement and event loop

I'm not sure to understand correctly the verticles principle. If my main class extends AbstractVerticle (implements the Verticle interface) and this class create instances of other class (by example, Controllers for MVC use case), the execution process of the functions in theses class is also asynchronous ? Even if theses class (controllers in my example) doesn't implements Verticle interface ?
I just want to have a full asynchonous execution in the same thread, i dont't want to use a worker verticle.
Thx for reading.
If you create an object as class variable in your class the extends AbstractVerticle then every method you would call with your object would be executed on your Verticle thread.
If your object is a synchronous class obviously this might block your thread execution.

ASP.NET Core MVC application dependency injection issue when using BaseController

Recently i tried to create a MVC application using ASP.NET Core 2.0 and i had some values defined in appsettings.json,
"MySettings": {
"WebApiBaseUrl": "http://localhost:6846/api/"
}
In order to read these values i have added
services.Configure<MySettingsModel>(Configuration.GetSection("MySettings"));
above line in ConfigureServices method in Startup.cs
and in my home controller i have added
private readonly IOptions<MySettingsModel> appSettings;
public HomeController(IOptions<MySettingsModel> app)
{
appSettings = app;
}
MySettingsModel class is just a model with property same as key define in appsettings.json.
by this method i'm able to read the value of this key.
Now my issue is that i want to use this key in many controllers so i don't want to repeat this code in every controller so what i did was i created a BaseConntroller, added its constructor and i got my values there. But when i inherit other controllers with my BaseController , it throws me an error and tells me to generate it's constructor, so basically it tells me to add constructor in every controller which is what i wanted to avoid.
How can i achieve this?
You can see the image for the error
And these are the potential fixes that it shows me.
This is just basic C# inheritance. Derived classes must re-implement constructors on base classes (at least the ones you want or need). The only exception is the empty constructor, which is implicit. In other words, you simply need:
public class HomeController : BaseController
{
public HomeController(IOptions<MySettingsModel> app)
: base(app)
{
}
And, of course, you need to change the accessibility of the base class field to protected instead of private. Otherwise, derived classes will not be able to access it.
Of course, this doesn't really save you that much. However, there's no free lunch here. Like I said, this is a limitation of C#, itself, so you have no choice. Although, it's worth mentioning, that while this can sometimes be annoying, it's actually a kind of useful feature of C#. You can look at any class and see exactly what constructors it has available, without having to trace down all its ancestors.
Actually, there is a good solution here:
https://stackoverflow.com/a/48886242/2060975
I am mostly using this method.
[Authorize]
[ApiController]
public abstract class ApiControllerBase : ControllerBase
{
private IOptions<AppSettings> _appSettings;
protected IOptions<AppSettings> appSettings => _appSettings ?? (_appSettings = (IOptions<AppSettings>)this.HttpContext.RequestServices.GetService(typeof(IOptions<AppSettings>)));
...
}
I hope it helps someone:)

RealmObject And Implement Interface

When I applied Realm in my object.
It look Like that:
public class Attribute extends RealmObject implements Parcelable, BaseEntity {}
Give me this error:
error: Only getters and setters should be defined in RealmObject classes
Who can give me the resons and solution. BIG THANKS.
Please help me !
Currently RealmObjects does not support implementing Parcable, and interfaces are only supported if they are empty or contain the getters and setter methods you would otherwise generate.
For Parcable you can consider using Parceler: https://realm.io/docs/java/latest/#parceler
Otherwise we are tracking the issue here: https://github.com/realm/realm-java/issues/878
Supporting interfaces in general will be possible once we implement support for custom methods, that can be tracked here: https://github.com/realm/realm-java/issues/909

Spring 3.2 #ControllerAdvice Not Working

I am having trouble getting #ControllerAdvice to work. I updated my namespace location, which were 3.1 in my xml files. I moved the class with the controller to the same package as the controller. I am using 3.2.0 release jars. If I put the #ExceptionHandler annotation in the controller code, it works, but not in a separate class with the #ControllerAdvice. When the #ControllerAdvice class fails, I get my uncaught exception handler view. Anyone have ideas on how to trouble shoot this one?
If you use classpath scanning, probably you have to add new include filter to your <context:component-scan> element:
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
Default scanning does not lookup this annotation, following spring-context-3.2.xsd for component-scan:
"Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided #Component, #Repository, #Service, and #Controller stereotypes will be detected."
For this problem, The first thing is confirming your config,
You need make sure that the #ControllerAdvice Class under your component-scan base package.
Make suer you use <mvc:annotation-driven/> in your spring-servlet.xml. or have #EnableWebMvc in your #ControllerAdvice Class
When you have the config right, the ControllerAdvice should already work, Now you said You got your uncaught exception handler view. I guess you got that in your InegrationTest, And you used mockMvc to test that, If so, you need put #WebAppConfiguration and build mokcMvc as follow:
#Autowired
private WebApplicationContext wac;
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
Using standaloneSetup(controller) will not work because lack of WebApplicationContext.
There is no extra configuration required. It should just work. Look at this link for more details. This provide very simple example:
https://javabeat.net/exception-controlleradvice-spring-3-2/
https://javabeat.net/controlleradvice-improvements-spring-4-0/
I had this same problem, in my case the problem was that there was a dependent library that had inside it a class with the #ControllerAdvice and #Order(Ordered.HIGHEST) annotation, to solve the problem I added the #Order(Ordered.HIGHEST) annotation in my classe, and now it works.
Since my exception class is in the same controller package spring gave my class higher priority even though both classes have the same #Order(Ordered.HIGHEST)
#ControllerAdvice
#Order(Ordered.HIGHEST)
public class RestResponseEntityExceptionHandler
extends ResponseEntityExceptionHandler {
I struggled with the same problem where my #ControllerAdvice class would not load while unit testing REST controllers' exceptions. If you are using spring boot (version 4) then you can use additional methods added by spring to load controller advice classes in standalone setting up your controllers.
mockMvc = MockMvcBuilders
.standaloneSetup(new YourRestController())
.setControllerAdvice(new ControllerAdviceClass())
.build();
This will straight-away initialize your controller advice class and your Junit test should be able to jump in to #ExceptionHandler methods defined in your controller advice class.
For me, #ControllerAdvice was not working at all cost. Even adding #EnableWebMvc or #WebAppConfiguration didn't make any change.
The way I was able make it working was,
adding #ExceptionHandler methods for my AbstractController class, the class that all the other controllers are extending upon.
I think #ControllerAdvice is supposed to do the same thing, i.e. compile the #ExceptionHandler methods defined under the class specified by #ControllerAdvice, into a common place where every controller can refer from. But unfortunately it was not working for me.
I solved it by defining ControlAdvice class in Configuration beans as shown below:
#Primary
#Bean
public RestResponseEntityExceptionHandler restResponseEntityExceptionHandler (){
return new RestResponseEntityExceptionHandler ();
}

javax.ejb.EJBException: Illegal non-business method access on no-interface view

I'm using EclipseLink on GlassFish 3.1.1 and I'm trying to understand this exception:
javax.ejb.EJBException: Illegal non-business method access on no-interface view
at org.mycompany.myproject.session.__EJB31_Generated__MyBeanFacade__Intf____Bean__.getEntityManager(Unknown Source)
at org.mycompany.myproject.session.AbstractFacade.edit(AbstractFacade.java:28)
at org.mycompany.myproject.controller.EditMyBeanServlet.doPost(EditMyBeanServlet.java:199)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
Notice that the stack trace indicates that the problem is triggered in the Netbeans generated AbstractFacade.getEntityManager method.
Any idea what the heck is going on or any tips to troubleshoot? I'm guessing that either the transaction or caching state in the EJB is weird when this happens because sometimes calling the edit method works fine. I'm calling the EJB methods from a Servlet. The exception occurs when trying to save changes to an entity.
The error you get most likely indicates that your code is trying to call the protected method anyway. This is not allowed for no-interface views on an EJB. You are only allowed to call public methods.
There's a small mismatch here between the normal Java class rules and the EJB rules. For a no-interface view, a proxy is created based on the original class type (typically a dynamic sub-class of it). This thus means that protected and package private methods are visible for code in the same package, and as far as the Java compiler is concerned, your code is allowed to call those.
But as mentioned, this is not allowed by the EJB rules, and thus an exception is thrown.
You can reproduce this easily by injection a bean like the following:
#Stateless
public class FooBean {
public void callMe() {
}
protected void doNotCallMe() {
}
}
Inject this somewhere (e.g. Servlet in same package) and try to call doNotCallMe(). You'll see the same exception. Call callMe() and everything will be fine.
I think I may have found a solution and possibly a bug in third party software. It seems like GlassFish 3.1.1 / EJB 3.1 / EclipseLink can't handle method overloading correctly. I've got a method defined in my EJB named edit that overloads (not overrides) the method from the parent abstract class. There is a method named edit in the abstract parent of the EJB that takes a generic type and then I've got a method named edit in the EJB which takes a List. If I rename the method to something else so that it is no longer overloading then the exception goes away!
Code:
public abstract class AbstractFacade<T> {
protected abstract EntityManager getEntityManager();
public void edit(T entity) {
...
and
#Stateless
public class MyEntityFacade extends AbstractFacade<MyEntity> {
protected EntityManager getEntityManager() { return em; )
public void edit(List<MyEntity> entities) {
...
Note: I noticed if I make the getEntityManager method public instead of protected I'll get a TransactionRequiredException instead of an EJBException.
What is weird is i had same problme with on inner class of my EJB.
While trying to call private method of parent or accessing on injected EJB, i faced some problems.
I had visibility on most of things but finally a runtie, things goes wrong.
Finally, i decided to retrieve my parent class throught JNDI, thus i could call public method without troubles. Meanwhile i could call still private methods on my parents class, i still have to remember that it will fail.

Resources