The following line fails with an exception:
HttpServletRequest req = mock(HttpServletRequest.class);
Exception:
java.lang.ClassFormatError: Absent Code attribute in method
that is not native or abstract in class file
javax/servlet/http/Cookie
Has anyone faced this problem?
UPD
Solution was found here.
You should add implementation of Servlet API to your test/runtime classpath. I would suggest to use this dependency:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
Related
I am trying to create a simple application that handles MongoDB database with MongoTemplate. However, this method:
#Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate template = new MongoTemplate(mongoClient(), this.mongodbName);
return template;
}
fails with this exception:
Error creating bean with name 'mongoTemplate' defined in class path resource [com/myapp/tryout/repository/config/MongoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: 'java.lang.String com.mongodb.connection.ClusterSettings.getDescription()'
pom.xml has these dependencies:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.5</version>
</dependency>
Spring version is 5.2.6.
Documentation of this com.mongodb.connection.ClusterSettings tells that the method in question is indeed deprecated. Moreover by looking in ClusterSettings class I cannot find this method.
My question is: Is it about mismatch in current releases of Spring and mongo-java-driver? If yes, can you please point out a correct combination of packages to use?
Please ask, if you need more information. I will gladly provide it.
ADDED: Ah, irony...
I found this
private static Cluster createCluster(final MongoClientSettings settings,
#Nullable final MongoDriverInformation mongoDriverInformation) {
notNull("settings", settings);
List<MongoCredential> credentialList = settings.getCredential() != null ? singletonList(settings.getCredential())
: Collections.<MongoCredential>emptyList();
return new DefaultClusterFactory().createCluster(settings.getClusterSettings(), settings.getServerSettings(),
settings.getConnectionPoolSettings(), getStreamFactory(settings, false), getStreamFactory(settings, true), credentialList,
getCommandListener(settings.getCommandListeners()), settings.getApplicationName(), mongoDriverInformation,
settings.getCompressorList());
}
in com.mongodb.client.internal.MongoClientImpl.
And this
public Cluster createCluster(final ClusterSettings clusterSettings, final ServerSettings serverSettings,
final ConnectionPoolSettings connectionPoolSettings, final StreamFactory streamFactory,
final StreamFactory heartbeatStreamFactory, final List<MongoCredential> credentialList,
final CommandListener commandListener, final String applicationName,
final MongoDriverInformation mongoDriverInformation,
final List<MongoCompressor> compressorList) {
ClusterId clusterId = new ClusterId(clusterSettings.getDescription());
in com.mongodb.connection.DefaultClusterFactory, which is deprecated.
Spring 3.x supports mongodb java version 4.x. The mongo-java-driver and mongodb-driver “uber-jars” are no longer published as noted in the linked page. Between 3.x and 4.x mongo driver java version the uber jar dependencies have been split to core and sync/reactive streams dependencies. I was able to reproduce the issue with 3.x version. Once I updated to correct 4.x dependencies issue disappeared.
So the correct dependencies are in the below order
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.0.4</version>
</dependency>
https://mongodb.github.io/mongo-java-driver/4.0/upgrading/#upgrading-from-the-3-12-java-driver
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#upgrading.2-3
Seems like I was correct about versions mismatch.
Changing versions of the mentioned packages to these ones:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.2</version>
</dependency>
will workaround the issue. I cannot mark this post as an answer to the issue. Seems like version 3.0.* of spring-data-mongodb is not compatible with the latest mongo-java-driver or vice versa. I can be wrong.
I am trying to change spring xml settings to pure code based setting.
So I read official documents and some posts from blogs.
e.g. http://docs.spring.io/spring-framework/docs/4.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html
An I made a code like ...
public class TestInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container)
throws ServletException {
// TODO Auto-generated method stub
System.out.println("on Startup method has called.");
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(RootConfig.class);
container.
//container.addListener(new ContextLoaderListener(ctx));
}
};
A problem here. In those pages, they use addListener(new ContextLoaderListener(ctx)) method to set context. However my eclipse can not find that method from container variable.
I do not know any clue why my container variable(javax.servlet.ServletContext instance) can not read this method.
Thanks for your answer:D
P.S.
My spring version is 4.1.6.RELEASE and I include servlet3.0, spring-context, spring-webmvc on pom.xml.
========================
Maybe I got some communication problem, So I summarize this :D
javax.servlet.ServletContext doc clearly state that it has method
addListener >>
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html
have to use Spring WebApplicationInitializer.onStartup(ServletContext) to set basic setting via Java source code, not XML
Can not load addListener from ServletContext class.
=================================
Edit. This is not error on console. However it is the only message I got.
It is from eclipse toolkit.
The method addListener(ContextLoaderListener) is undefined for the type ServletContext
than recommendation is Add cast to 'container'
To follow up on what #JuneyoungOh has commented, turns out that the problem is because of conflicting dependency. And these are the ways to solve this problem :
* make version 3.0.1 and artifactId 'javax.servlet-api' or
* add tomcat(in my case 7.0) to project build path and remove servlet dependency.
In my case the problem was because of Spring-Support which is depended on "javax.servlet" and I just excluded it:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
<version>${spring-support.version}</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
In my case there was:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
notice, that artifactId is servlet-api, not javax.servlet-api.
I have created a legacy MVC project, that's why I had this package. When I tried to convert .xml configuration to Java, I came across this problem.
Certainly it's not the same as in the question, but it shows up as the first result in google search.
In my case I just had to comment out the javax.servlet:servlet-api dependency as depicted here:
<!-- dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.47</version>
</dependency>
This looks like the same idea presented here: https://stackoverflow.com/a/30231246/2597758
I have a JAX-RS webservice using Jersey-1. There is only a single method at the moment, which sends mails with attachment. The attachment has to be provided as a multipart form data.
#POST
#Path("/mail")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response sendInfomailWithAttachment(
#Context ServletContext context,
#Context SecurityContext security,
#QueryParam ("FROM") String senderAddress,
#QueryParam ("SUBJECT") String subject,
#QueryParam ("TO") String toRecipients,
#QueryParam ("CC") String ccRecipients,
#QueryParam ("BCC") String bccRecipients,
#QueryParam ("noCopy") boolean sendNoCopy,
#FormDataParam("attachment") InputStream fileInputStream,
#FormDataParam("attachment") FormDataContentDisposition contentDispositionHeader,
#FormDataParam("attachment") FormDataBodyPart fileBody,
#FormDataParam("content") FormDataBodyPart content
){}
The method works fine. But when I try to integrate swagger, I always get this error(s):
SEVERE: Missing dependency for method public javax.ws.rs.core.Response ... throws java.lang.Exception at parameter at index 8
(this error is thrown for param 9, 10 and 11 as well)
in combination with
SEVERE: Method, public javax.ws.rs.core.Response ... throws java.lang.Exception, annotated with POST of resource, class com.mywebservice.MyClass, is not recognized as valid resource method.
If I throw out the FormDataParam stuff, the service starts normally, also with the swagger integration.
I use
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.17</version>
</dependency>
and
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey-jaxrs_2.10</artifactId>
<version>1.3.4</version>
</dependency>
Is there any known interference? Anything else I could do to come around this?
As I found out, it was a maven resolution problem.
The swagger lib 1.3.4 uses jersey-core-1.3 and jersey-server-1.3, which has been chosen by maven. Instead, I needed it in 1.17 like the jersey-servlet and jersey-multipart version. So I had to add those two dependencies to the pom to make the maven dependency resolution use 1.17 over 1.13.
I'm trying to use Hibernate Validator in my project, but it isn't working. On the following line:
SessionFactory sessions = config.buildSessionFactory(builder.build());
I get the following exception:
org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:154)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at net.myProject.server.util.HibernateUtil.<clinit>(HibernateUtil.java:32)
... 36 more
Caused by: java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider;
at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:119)
at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:45)
at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:217)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
I found this question which seems quite similar to my problem. He describes his solution as
I had yet another bean validator jar in the class path. But not from
maven, so i didn't realize it. Removing that solved the problem.
I think my problem is the same. On http://hibernate.org/validator/documentation/getting-started/ it says:
This transitively pulls in the dependency to the Bean Validation API
(javax.validation:validation-api:1.1.0.Final)
That must be causing this issue, since reverting to an older version (4.3.1.Final) fixes the issue. Is there a way to force Hibernate to not pull in the Bean Validation API?
Edit: I've tried to exclude the javax-validation api:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.3.Final</version>
<exclusions>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
But it didn't seem to have any effect.
Try adding this dependency to your pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
If not consider using hibernate-validator4.2.0.Final I have that one in my config and it is working fine.
For me, the 1.1.0.Final version javax.validation.validation-api had worked. Because, the javax.validation.spi.ConfigurationState interface of 1.1.0.Final has getParameterNameProvider method, which was absent in 1.0.0.GA.
I added the below dependency in pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>test</scope>
</dependency>
I had the problem again. Thats how I've fixed that:
1-Exclude spring.validator from the 'web' dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
2-After insert the dependecy with a previous version:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
in my case i just deleted the hibernate-validator and it worked .(i also had a combo of both validation api and hibernate-validator and tried everything) or you can go to your maven repository-->org and then delete the hibernate folder and rebuild your project again..
hope it helps..
I thought it would be useful to explain what is going on here.
Hibernate is calling ConfigurationState.getParameterNameProvider:
ValidatorFactoryImpl.java:
public ValidatorFactoryImpl(ConfigurationState configurationState) {
...
configurationState.getParameterNameProvider()
...
}
You can find the documentation of getParameterNameProvider:
getParameterNameProvider
ParameterNameProvider getParameterNameProvider()
Returns the parameter name provider for this configuration.
Returns:
parameter name provider instance or null if not defined
Since:
1.1
So what's the problem? The problem is that the method didn't always exist. It was added at some point in the future.
And the rule when creating interfaces is that they are set in concrete: you shall not change an interface ever. Instead the JavaX validator changed the ConfigurationState interface, and added a few new methods over the years.
The java validation code is passing the Hiberate an outdated ConfiguationState interface; one that doesn't implement the required interfaces.
You need to ensure that javax.validation.Validation.buildDefaultValidatorFactory is updated to to support version 1.1.
Removing this jar javax.validation:validation-api:1.1.0.Final solved my problem.
Make sure you have only one validation jar. If we have two jars then they may conflict resulting in error.
Go to the dependecies project and delete, hibernate.validator, and reinstall that in the most recent version. It has solved the problem for me.
I have a Spring 3 MVC app that I am setting up some ajax actions for.
My controller action looks like this:
#RequestMapping(value="add", method=RequestMethod.POST)
#Secured("ROLE_USER")
#ResponseStatus(HttpStatus.CREATED)
public #ResponseBody Plan addPlan(#RequestBody Plan plan, Principal principal) {
//Save the plan
}
When I post the Plan data from my browser the app throws a ClassNotFound exception:
java.lang.ClassNotFoundException: org.joda.time.ReadableInstant not found by jackson-mapper-asl [176]
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:787)
at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:71)
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1768)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
The Plan object itself does not contain any joda-date types. Though it contains a collection of objects that do. Originally I was pulling in the joda-date jar via my DOA jar but the error persists even if I add a direct dependency to my web project's pom.xml. I'm using the joda classes elsewhere in this project without any issue.
Additional information
Here are the relevant dependencies from my web pom.xml:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
I somehow came across this question: Apache FTP server is not seeing a logging jar package that exists in the class path
Their solution of setting <class-loader delegate="false"> in glassfish-web.xml seems to have fixed my issues.
I've reported this on Glassfish JIRA https://java.net/jira/browse/GLASSFISH-20808