Eclipse not recognizing slf4j or logback Logger class methods - eclipse

I have recently been tasked to begin architecting a new Spring 3 MVC project (I am quite the noob in this regard). Having set up most of the POC project in Kepler, I have:
a prototype webapp (HelloWorld style) that is deployable and
working in (an external) Tomcat7.
Logback configured and reporting to the console:
...
12:46:21.916 [http-apr-8080-exec-5] TRACE o.s.w.s.v.InternalResourceViewResolver - Cached view [home]
12:46:21.916 [http-apr-8080-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB-INF/view/home.jsp]] in DispatcherServlet with name 'sample'
12:46:21.916 [http-apr-8080-exec-5] TRACE o.s.web.servlet.view.JstlView - Rendering view with name 'home' with model {name=lingxotika.org} and static attributes {}
12:46:21.916 [http-apr-8080-exec-5] DEBUG o.s.web.servlet.view.JstlView - Added model object 'name' of type [java.lang.String] to request in view with name 'home'
12:46:21.921 [http-apr-8080-exec-5] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/view/home.jsp] in InternalResourceView 'home'
12:46:21.957 [http-apr-8080-exec-5] TRACE o.s.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#1bb3db1b
12:46:21.960 [http-apr-8080-exec-5] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
12:46:21.960 [http-apr-8080-exec-5] TRACE o.s.w.c.s.AnnotationConfigWebApplicationContext - Publishing event in WebApplicationContext for namespace 'sample-servlet': ServletRequestHandledEvent: url=[/codetutr/home/]; client=[127.0.0.1]; method=[GET]; servlet=[sample]; session=[0108E781644C1268FD92C11BD78888FB]; user=[null]; time=[87ms]; status=[OK]
but when I go into my code into use the Logger class methods (I've tried both
org.slf4j.Logger and ch.qos.logback.classic flavors), Eclipse
does not recognize the methods in the Logger instance:
package org.lingxotika.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class SampleController {
static final Logger log = LoggerFactory.getLogger(SampleController.class);
log.info("Hit Controller..."); // Error is here
#RequestMapping("home")
public String loadHomePage(Model model) {
model.addAttribute("name", "lingxotika.org");
return "home";
}
}
The Eclipse Code Assist after typing log. shows the following options (sorry, can't post screenshots yet):
new - create new object
nls - non-externalized string marker
runnable - runnable
toarray - convert collection to array
To sum, I have seen examples here and here that suggest this isn't a limitation of Spring or my admitted lack of competency with it, but an Eclipse issue that I am overlooking. I avoided posting what could be irrelevant information, but am happy to supply any further code, logs, or config examples. Any help would be appreciated.

Seems like you dont have the slf4j api jar in your project.
Since it is working in tomcat, perhaps tomcat is using a "provided" jar, ie: one that is part of the tomcat application.
If you are using maven, you need to have the following dependency in you pom:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

Related

Eclipse custom #NonNull annotation ignored

I would like to use the "null analysis" feature of Eclipse but I would rather use the javax.annotation.Nonnull annotation than the org.eclipse.jdt.annotation.NonNull annotation. I can't seem to get Eclipse to recognize any custom annotations I configure.
I'm using Eclipse Luna 4.4.2.
I created a new test project that contains only the following class:
package org.example;
import javax.annotation.Nonnull;
public class OmgNulls {
public static void main(String[] args) {
#Nonnull String test = null;
System.out.println(test);
}
}
My compiler settings are configured to use the javax annotations:
My project's includes the checker-1.8.10 JAR to provide the annotations.
With these settings, Eclipse reports 0 errors.
If I check "Use default annotations for null specifications" and change my test class to use Eclipse's annotation, I get:
Anyone know what I'm doing wrong or is this an Eclipse bug maybe?
I've also tried the jsr305 JAR from google instead of checker in case that was an issue but that didn't help.

Customised ObjectMapper not working with spring boot hateoas

I have developed an rest service using spring-boot and Spring-boot-starter hateoas. And I am facing an issue with customizing ObjectMapper. The code goes below for the same:
Application.java
#Configuration
#Import(BillServiceConfig.class)
#EnableAutoConfiguration
#EnableEurekaClient
#ComponentScan({"com.bill"})
#EnableWebMvc
#EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application extends WebMvcConfigurerAdapter{
#Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("MM-yyyy-dd"));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
builder.configure(objectMapper);
return builder;
}
Dependencies:
dependencies {
compile "org.springframework.boot:spring-boot-starter-hateoas"
compile "org.springframework.boot:spring-boot-starter-ws"
compile "org.springframework.boot:spring-boot-starter-actuator"
Bill.java:
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonRootName("bills")
public class Bill{
BillController.java:
public ResponseEntity<Resources<Resource<Bill>>> getBills(){
The output I am getting is:
{
_embedded: {
billList:
But I require "bills" in place of "billList". It is because of ObjectMapper is not getting customized. Am I missing any configuration, Kindly help me out in this issue. Thanks in advance.
I'm using spring-boot 1.5 RC1. If you remove the #EnableHypermediaSupport annotation spring-boot should configure spring-hateoas with ISO 8601 dates for you so long as you have java time module on the classpath.
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
This worked for me anyway.
If you want further custom configuration see the solutions at http://github.com/spring-projects/spring-hateoas/issues/333
Root of this problem - default ObjectMapper from Spring MVC is used instead of one configured by author.
This happens because of #EnableWebMvc.
Quote from Spring Boot guide
Normally you would add #EnableWebMvc for a Spring MVC app, but Spring
Boot adds it automatically when it sees spring-webmvc on the
classpath.
However if you one puts it, Spring MVC will create its own set of MessageConverters and won't use yours ObjectMapper.
PS even though I post this answer so late, may be it will help others.

WELD-001408 Unsatisfied dependencies for type [DataSource] with qualifiers

I have the following that is causing the subjected error message in APP-A
#Inject #CtcDB
private DataSource ds;
I'm using an identical pattern with APP-B which does not complain of this error.
When I hover over ds in Eclipse while holding down the ctrl key, I get the following context menu for APP-B
Open #Inject Bean Resource.ds
Open Declaration
Open Declared Type
but in the APP-A with the dependency error I only get
Open Declaration
Open Declared Type
// The Resources class is basically the same b/n both apps except the qualifier is #CommitmentDB for APP-B
public class Resources {
#Produces
#Resource(mappedName="java:jboss/datasources/myjndids")
#CtcDB
private DataSource ds;
// And my annotation
#Qualifier
#Retention(RUNTIME)
#Target({ METHOD, FIELD, PARAMETER, TYPE })
public #interface CtcDB{
}
I know CDI is active (I have a beans.xml in WEB-INF) because I have a POM dependency which contains some injectable beans. When hovering with the ctr key for those injections I get the expected context menu (in both apps)
OK I figured it out myself. In the Resources class I had the wrong import for DataSource
import javax.activation.DataSource; \\instead of
import javax.sql.DataSource;
I must have clicked on the wrong Eclipse Quickfix and imported the wrong package.

embedded Jetty in Eclipse RCP

I have a problem for embeding Jetty into my Eclipse RCP application.
In my RCP application, when user click some button, a browser will be opened and some jsp page shown. The jsp files are in a separated directory, it is a web application, which can be run in tomcat very well.
I have managed this with in a main() method like this:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class SimplestServer
{
public static void main(String[] args) throws Exception
{
int port = 8080;
Server server = new Server(port);
String webapp = "D:/workspace/preview";
WebAppContext context = new WebAppContext();
context.setDefaultsDescriptor(webapp + "/WEB-INF/web.xml");
// -------
// Sorry! I add another question in one post, I think this might be some clue
// If I do not use setDefaultsDescriptor, I got error like this:
// java.io.FileNotFoundException: D:\BV\eUpgrade\testEnv\eclipse4-2\org\eclipse\jetty\webapp\webdefault.xml
// Why it does not go to my web.xml, but goes to some path like: org\eclipse\jetty\webapp\webdefault.xml?
// And in this case, when access to jsp files, got HTTP 503 error.
// context.setDescriptor(webapp + "/WEB-INF/web.xml");
// ------
context.setResourceBase(webapp);
context.setContextPath("/preview");
context.setParentLoaderPriority(true);
server.setHandler(context);
try {
server.start();
server.join();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
It works well for me if just in a main method. i.e., after Jetty started, I can access to all of the pages in my web app.
But when I put this snippet into my plugin, it does not work.
I created a sample eclipse rcp project (with the mail template), and I put the above code into my Activator.java. Then I start the eclipse application, what I saw is some error like :
...
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded interface javax.servlet.Filter
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded interface javax.servlet.Filter from org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader#2a49d3b5[javax.servlet:3.0.0.v201112011016(id=4)]
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class java.lang.Object
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class java.lang.Object from null
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class com.broadvision.ssp.webflow.SetCharacterEncodingFilter from WebAppClassLoader=855125537#32f82e21
19:01:03.762 [main] DEBUG org.eclipse.jetty.servlet.Holder - Holding class com.broadvision.ssp.webflow.SetCharacterEncodingFilter
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class java.lang.Throwable
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class java.lang.Throwable from null
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class java.lang.Exception
19:01:03.762 [main] DEBUG o.e.jetty.webapp.WebAppClassLoader - loaded class java.lang.Exception from null
...
It seems that only the classes in my WEB-INF\lib*.jar can be loaded, those classes in JRE cannot be loaded in runtime.
The result is:
The Jetty server is up (I checked the port is being used by javaw.exe), but all of the page returns: HTTP 404 error. The web application has not been deployed successfully.
I have read these:
Embed Jetty in Eclipse RCP
Eclipse RCP plugin + embedded Jetty + JSF
https://stackoverflow.com/questions/12530256/eclipse-rcp-with-jetty-integration
But I cannot find the answer for my question.
Any help would be appreicated!! Thanks in advance.
To get started with an embedded jetty server start with the most basic tutorials. These will guide you to serve a single static HTML file.
Eclipse Wiki: Jetty/Tutorial/Embedding Jetty
Official Jetty Tutorial
Once you got this working you can move on to expand stuff like serving an application.

GWT 2.4: "An internal compiler exception occurred" in project that uses Hibernate for Bean Validation

It's been about 5 hrs since I decided to use JSR 303 Bean Validation in my GWT project and I gotta say I can't even express (politely) how deeply unsatisfied I am with lame documentation on the subject on Google's website.
I really hope you guys can help me.
I followed this blog post to add client-side bean validation to my project. Unfortunately it worked only once and threw an exception in runtime saying that I need to add Hibernate Validation sources to class path. I fixed that and decided to remassage my dependencies a little too (biggest mistake of my life) but I couldn't make it work ever again.
I can't play with Validation sample from GWT SDK either because it's uncompilable because it has two implementations of class ServerValidator. Weird.
So to simplify my question I created dummy GWT application using project wizard of IntelliJ IDEA.
I added following elements to module xml:
<inherits name="org.hibernate.validator.HibernateValidator"/>
<replace-with class="com.mySampleApplication.client.ClientValidatorFactory">
<when-type-is class="javax.validation.ValidatorFactory"/>
</replace-with>
Created ClientValidatorFactory:
package com.mySampleApplication.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.validation.client.AbstractGwtValidatorFactory;
import com.google.gwt.validation.client.GwtValidation;
import com.google.gwt.validation.client.impl.AbstractGwtValidator;
import javax.validation.Validator;
import javax.validation.groups.Default;
public class ClientValidatorFactory extends AbstractGwtValidatorFactory
{
#GwtValidation(value = {Organization.class}, groups = {Default.class, ClientGroup.class})
public interface GwtValidator extends Validator
{
}
#Override
public AbstractGwtValidator createValidator()
{
return GWT.create(GwtValidator.class);
}
}
And in onModuleLoad() method I added this single line which causes compiler to blow up
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
And finally I used following jars which I copied from Validation sample of GWT SDK.
hibernate-validator-4.1.0.Final-sources.jar
hibernate-validator-4.1.0.Final.jar
log4j-1.2.16.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
validation-api-1.0.0.GA-sources.jar
validation-api-1.0.0.GA.jar
But when I compile my project it gives following meaningless error:
In detailed GWT compiler log I see this:
Loaded 2315 units from persistent store.
Found 2282 cached units. Used 2282 / 2282 units from cache.
Added 0 units to persistent cache.
Validating newly compiled units
Errors in 'jar:file:/C:/work/externals/gwt/gwt-user.jar!/org/hibernate/validator/engine/ConstraintViolationImpl_CustomFieldSerializer.java'
Line 33: No source code is available for type org.hibernate.validator.engine.ConstraintViolationImpl<T>; did you forget to inherit a required module?
Errors in 'jar:file:/C:/work/externals/gwt/gwt-user.jar!/org/hibernate/validator/engine/ValidationSupport.java'
Line 43: No source code is available for type org.hibernate.validator.engine.ConstraintViolationImpl<T>; did you forget to inherit a required module?
Errors in 'jar:file:/C:/work/externals/gwt/gwt-user.jar!/org/hibernate/validator/super/org/hibernate/validator/constraints/impl/EmailValidator.java'
Line 25: No source code is available for type org.hibernate.validator.constraints.Email; did you forget to inherit a required module?
Errors in 'jar:file:/C:/work/externals/gwt/gwt-user.jar!/org/hibernate/validator/super/org/hibernate/validator/constraints/impl/ScriptAssertValidator.java'
Line 26: No source code is available for type org.hibernate.validator.constraints.Email; did you forget to inherit a required module?
Errors in 'jar:file:/C:/work/externals/gwt/gwt-user.jar!/org/hibernate/validator/super/org/hibernate/validator/constraints/impl/URLValidator.java'
Line 26: No source code is available for type org.hibernate.validator.constraints.URL; did you forget to inherit a required module?
Errors in 'jar:file:/C:/work/externals/gwt/gwt-user.jar!/org/hibernate/validator/super/org/hibernate/validator/engine/PathImpl.java'
Line 72: No source code is available for type org.hibernate.validator.engine.NodeImpl; did you forget to inherit a required module?
Why can't it find classses? I have hibernate-validator-4.1.0.Final-sources.jar in my classpath.
Any thoughts ?
I uploaded my project here if you guys want to play with it.
Case closed, guys. Error was caused by lack of hibernate validation sources in classpath because of bug in IntelliJ IDEA. Details are here.