Dropwizard and Jersey SSE (Server side events) - java

Does anyone used this Jersey SSE feature already in a Dropwizard application? I tried it with DW 0.9.1 but all I get is this Exception at startup:
[main]: /:?:?- unavailable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public org.glassfish.jersey.media.sse.EventOutput .......
Authentication and Authorization is also done like here with SecurityContext. I tried also a very simple no authentication/authorization app and there is the same problem. Perhaps I have to register SSE, but I dont know how if this solves my problem, because Jersey documentation says that there is a automated registering since version 2.8.x.
What i tried, but does not help:
environment.jersey().register(SseFeature.class);
environment.jersey().getResourceConfig().register(SseFeature.class);
System.out.println("sse feature:" + environment.jersey().getResourceConfig().getConfiguration().isRegistered(SseFeature.class));
Output:
...
sse feature:true
WARN [2015-11-11 10:12:58,845] [main]: org.glassfish.jersey.internal.Errors:?:?- The following warnings have been detected: WARNING: Cannot create new registration for component type class org.glassfish.jersey.media.sse.SseFeature: Existing previous registration found for the type.
...
Does anyone know how to solve this problem or have any ideas?

This is a really strange problem. Dont know why it works now. Perhaps if others have the same problem, I have done this:
environment.jersey().register(new EventOutputMesssageBodyWriter());
The MsgBodyWriter does no write (writeTo method is empty), but it works. Really strange ...

Related

Is there a way to enable Spring-Boot to reveal more information about 'BeanCreationException: Error creating bean with name'?

I have been working with Spring for 8 years now, and very often I run into this exception.
BeanCreationException: Error creating bean with name
For example, I am working on a service right now that fails to start because a Bean requires a connection to Azure (on AMQP port) to be successful. The stack trace says nothing about the cause of the error though. Enabling com.azure.core or org.apache.http logging DEBUG also not helping.
Is it possible to intercept the Bean instantiation to reveal more information about the cause of the error? Is there a Spring coding pattern for this kind of thing?
Would love to solve this once and for all so that users of services I create are never confused about a Bean loading exception.
This page elaborates on the issue: https://www.baeldung.com/spring-beancreationexception but never tells you what to do when dealing with Beans being loaded from a 3rd party lib rather than your own code.

Handle # character in spring web

I am trying to add an exception for security rules in Spring but getting 401 meaning my mapping is not recognized. The uri is /test/acc#v1=1&v2=2...
I have security configured:
http.authorizeExchange().pathMatchers("/test/acc*{v:.+}")
and
http.authorizeExchange().pathMatchers("/test/acc*")
And my controller annotation is:
#GetMapping("/test/acc{suffix:.+}")
None of it works, I keep on getting 401. Can someone help me out here?
It is a general consensus that Server does not receive the URL fragmentation details and it is applicable for all major servers java based servers like tomcat, jetty..etc please refer for more details

RMI, Wildfly 10x, Spring: NoClassDefFoundError: org.omg.CORBA.COMM_FAILURE

I have a Spring-Boot application, which exposes some of its Interfaces to other projects. The basic process of this has been tried and tested within the project structure very often, so this SHOULD not be a "simple" RMI issue like missing serialization or incorrect registry.
This project (coded on java7) and an older project (also java7) are to be deployed in a wildfly 10.x container.
Basically, it is planned that our old application talks to the Spring-Boot application to get specific data sets. When starting the application, the beans are instantiated correctly, and the basic connection seems to be okay. However, when using an entirely valid call, I get this:
ERROR [io.undertow.request] (default task-5) UT005023: Exception handling request to /servlet/Foo: org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/omg/CORBA/COMM_FAILURE
I tried debugging the application, and when using the offending method call, the remote class is being triggered and does its work normally, the return value is as expected and is being returned to the spring context.
The weird thing is this part of the Stack Trace:
java.lang.NoClassDefFoundError: org/omg/CORBA/COMM_FAILURE
at org.springframework.remoting.rmi.RmiClientInterceptorUtils.isCorbaConnectFailure(RmiClientInterceptorUtils.java:229)
at org.springframework.remoting.rmi.RmiClientInterceptorUtils.isConnectFailure(RmiClientInterceptorUtils.java:213)
at org.springframework.remoting.rmi.RmiClientInterceptor.isConnectFailure(RmiClientInterceptor.java:282)
at org.springframework.remoting.rmi.RmiClientInterceptor.doInvoke(RmiClientInterceptor.java:347)
at org.springframework.remoting.rmi.RmiClientInterceptor.invoke(RmiClientInterceptor.java:259)
because when I try to access RmiClientInterceptorUtils.java:229, Eclipse tells me that 229 is not a valid line number. This would lead me to believe that there is a versioning difference in Spring webMVC between server and client. This persisted, even after changing the version number (now using 3.2.8.RELEASE of spring-webmvc), the errors persisted.
I checked if there might be a dependency problem, but CONN_FAILURE is part of rt.jar and should, as far as I know, be present in every java project. Further testing revealed that:
Checked Exceptions are transferred normally
primitive values (like boolean) are transferred correctly
complex Objects cause the exception to be thrown
I'd appreciate any help with this matter, as I am at a loss to explain or correct this.
Thanks.
NOTE:
The general reason for the raised exception has been found - a class was missing the Serializable interface. This does, however, not answer the question why the COMM_FAILURE raised a NoClassDefFoundError, which is something I still would like to know.

Spring Webapp throwing 404 errors with no console log

I am working on a web application using the Spring framework and Hibernate. My problem is that I often receive 404 errors caused by a mistake I have made somewhere in the codebase but there are no exception messages in the console. Because of this I am struggling to find where the mistake is because the project has become very large and manually trying to find the problem is impractical. I'm assuming that Spring is causing the problem so my question is: is there some way of enabling more detailed error messages? Thanks
404 is an http error and only your web server might be knowing of it. Very likely with these failed requests, your application server or Spring container was never hit. Look for web server logs to identify the problem.
Troubleshooting 404 on IIS server
http://blogs.iis.net/tomkmvp/archive/2009/04/27/troubleshooting-a-404.aspx
Troubleshooting 404 on RAD web server
http://www-304.ibm.com/support/docview.wss?uid=swg27035752&aid=1
As a couple of people have already alluded to it, the issue here is that certain errors (like the 404 exception) get intercepted by the Servlet container, therefore never reaching Spring and whatever logging mechanisms you have may have set up. So the trick here is to change the order of importance of your Exceptions so that Spring gets a crack at it.
The best approach I have ever come across to catch, handle and adequately log all exceptions in Spring is described in this article: http://steveliles.github.io/configuring_global_exception_handling_in_spring_mvc.html
I have been implementing this setup since I came across that blog post, and it has been a lifesaver to say the least. It will give you the detailed error messages you need. The key is to basically create a custom Exception Handler by implementing Spring's HandlerExceptionResolver and Ordered interfaces, then returning the lowest possible order number, thus moving your exception handling up the totem pole:
import org.springframework.core.*;
import org.springframework.web.servlet.*
public class LoggingHandlerExceptionResolver
implements HandlerExceptionResolver, Ordered {
public int getOrder() {
return Integer.MIN_VALUE; // we're first in line, yay!
}
public ModelAndView resolveException(
HttpServletRequest aReq, HttpServletResponse aRes,
Object aHandler, Exception anExc
) {
anExc.printStackTrace(); // again, you can do better than this ;)
return null; // trigger other HandlerExceptionResolver's
}
}
The problem ended up being that there were a couple of missing annotations from one of my Hibernate entities. Following the procedure from the link below helped track it down by providing more detailed error messages:
http://www.captaindebug.com/2011/07/using-hibernate-validation-annotation.html
I also hit the problem of no console output while 404 error.
As in The Saint's answer, one of the causes of no console log:
the issue here is that certain errors (like the 404 exception) get intercepted by the Servlet container
"spring-framework-reference-3.2.3.pdf" --> 1.3 --> Logging --> Using Log4J, solved the problem in my environment.

Stop RestTemplate logging warning on 404 not found

We have a service based platform where its possible (/common) for a resource not to be found - e.g. calling our security module with the wrong username will return 404 Not Found, something that will happen every time a user makes a typo in their login box.
We use Spring RestTemplate for these calls, which is great, but every time a 404 is encountered it dutifully logs a warning which is spamming our logs.
We obviously don't want to suppress warnings, except in the specific case of 404 not found but there doesn't appear to be a way to do this (logger is private/final, method to invoke it is private etc).
Our solution is not good - to return 200/OK with empty dataset and handle a null pointer, which is both nasty and not a good restful implementation.
Does anyone know a better way to do this?
How about using a RegexFilter filter on your logging appender?
http://logging.apache.org/log4j/2.x/manual/filters.html#RegexFilter
... that's the Log4J way of doing it, but I'm guessing there must be similar filters for other logging libraries.
Implement a ResponseErrorHandler that returns false for hasError().
Assign it to your rest template using setErrorHandler().
The warning will not be logged.
This was logged in as issue SPR-12760 in the issue tracker of Spring Framework and resolved earlier this year (2015) in version 4.1.6. The solution was twofold: first, the warning message was downgraded to a debug message, and second, it was made easier to override the part of the code that is responsible for handling errors. So as a solution to your problem, upgrade the org.springframework:spring-web module to at least version 4.1.6.RELEASE.

Categories

Resources