I have clear maven web app project.
I add jUnit to pom.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library # http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
</dependency>
</dependencies>
I have one very simple Servlet in my app.
I want to generate jUnit 4 test for this servlet.
So I click on the Servlet package > Tools > Create Test.
After this I get test class with method.
public void testProcessRequest() throws Exception {
System.out.println("processRequest");
HttpServletRequest request = null;
HttpServletResponse response = null;
MyServlet instance = new MyServlet(); // This is the problem
instance.processRequest(request, response);
// TODO review the generated test code and remove the default call to fail.
//fail("The test case is a prototype.");
}
MyServlet is underline and the error is : "Java EE API is missing on the project classpath".
I tried modify the scopes of dependencies in pom but problem still exist.
Related
So I was using Open Tracing to trace calls to the microservice. I used to do it the following way:
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>0.31.0</version>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-cloud-starter</artifactId>
<version>0.1.13</version>
</dependency>
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
<version>0.31.0</version>
</dependency>
Then add following bean:
#Bean
public Tracer initTracer() {
Configuration.SamplerConfiguration samplerConfig = new Configuration.SamplerConfiguration().withType("const").withParam(1);
Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv().withLogSpans(true);
return Configuration.fromEnv("my-service").withSampler(samplerConfig).withReporter(reporterConfig).getTracer();
}
When running I usually get these span reported logs whenever an external request is given to the application.
But with open telemetry I cannot simply do this anymore. What is the correct way to simulate the behaviour?
I am starting with SpringBoot, looks great, but I have some questions that I can't understand or find explained in the docs.
I created a new project with the Web, JPA, Security and MySQL dependencies. When my project is created, I go to create a #Controller class. Spring don't find #RequestMapping or ModelAndView classes.
I guessed that use the Web module of SpringBoot will add all the necessary dependencies to use SpringMVC (I read some examples and none add extra dependencies) and all work great with MVC.
These are my dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-groovy-templates</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Utils -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
</dependencies>
Other example, is the WebMvcConfigurerAdapter (from spring-mcv) class that I can't resolve:
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
This class is from, but I don't see Springboot include this dependy:
org.springframework
spring-webmvc
Maybe I am wrong and read some post that center all the info in Spring Boot, but don't show manual config in the poms.
WebApplication class (Auto Generated):
#SpringBootApplication
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
ServletInitializer.java class (Auto generated)
public class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
}
Looks like my maven local repository is corrupted.
I was using these libraries in other projects and all works fine. Sorry for the time wasted. Anyway, I let the response for some users with similar problems.
I am having trouble using swagger with Apache CXF, JAX-RS services.
beans.xml:
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>
<bean id="apiListingResourceJSON" class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON"/>
<bean id="apiDeclarationProvider" class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider"/>
Example use:
#Path("/")
#Api(value="/", description="VenturoSoft eMustering Services")
public class Service {
final static Logger logger = Logger.getLogger(Service.class);
#GET
#Path("/echo/{input}")
#Produces(MediaType.TEXT_PLAIN_VALUE)
#Consumes(MediaType.TEXT_PLAIN_VALUE)
#ApiOperation(value = "Get Ping", response = String.class)
public String ping(#PathParam("input") String input) {
return PingImpl.ping(input);
}
Pom.xml:
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.10</artifactId>
<version>1.3.12</version>
</dependency>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
Run:
mvn tomcat7:run-war
But when I load:
http://localhost:13000/jaxrs-service/api
I get no response.
http://localhost:13000/jaxrs-service/echo/echoSomething
Works as desired.
You're looking at some very old dependencies. From your code, it looks like you're using JAXRS. If that's the case, the latest dependencies should be:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.4</version>
</dependency>
Please follow the JAXRS sample here:
https://github.com/swagger-api/swagger-samples/tree/master/java/java-jaxrs-cxf
Which should show you how to correctly configure the server.
I'm having trouble creating a JUnit test using a pom.xml dependency.
The test are being run with Arquillian
#RunWith(Arquillian.class)
In this method
#Deployment
public static JavaArchive createDeployment() {
First, I create a JavaArchive with the package of the project I'm testing
JavaArchive merge = ShrinkWrap.create(JavaArchive.class).
addPackages(true,
"migrazioneGeaPersistenzaTampone",
"migrazioneGeaPersistenza",
"it.**.mistral.importGEA4.task",
"migrazioneGeaPersistenzaAccess"
).
addClasses(java.sql.Connection.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
Then when I launch the test, there are some missing dependencies
Unable to resolve any beans for Types: [class it.**.**.be.service.EnvironmentRootService]
present in this dependency
<dependency>
<groupId>it.**.mistral</groupId>
<artifactId>mistral-be</artifactId>
<version>0.1.0</version>
<scope>compile</scope>
</dependency>
I have tried lots of different things to add these dependencies, the best one seems to be using the ShrinkWrap Resolvers (https://github.com/shrinkwrap/resolver/blob/master/README.asciidoc, particullarry this paragraph https://github.com/shrinkwrap/resolver/blob/master/README.asciidoc#resolution-of-artifacts-defined-in-pom-files)
a)
JavaArchive[] archives = Maven.resolver().loadPomFromFile(path_to_pom_file).
importDependencies(ScopeType.TEST,ScopeType.COMPILE).
resolve().withTransitivity().as(JavaArchive.class);
or
Maven.resolver().loadPomFromFile("/path/to/pom.xml").importRuntimeDependencies()
.resolve().withTransitivity().asFile();
The dependency is ignored in either way (i'm missing something?)
b)
JavaArchive[] mistral_be = Maven.configureResolver().workOffline().
resolve("it.**.mistral:mistral-be:0.1.0").withTransitivity().as(JavaArchive.class);
for (int i = 0; i < mistral_be.length ; i++) {
merge = merge.merge(mistral_be[i]);
}
With a simple
System.out.println(merge.toString(true));
I can see that all files from dependencies are present! Anyway I use local repository workoffline()
Maybe I am missing some dependencies?
But exiting the method with a
return merge;
throws a "java.lang.NoClassDefFoundError: com/google/protobuf/GeneratedMessage....Caused by: java.lang.ClassNotFoundException: com.google.protobuf.GeneratedMessage" error.
Then again i tried to add the missing dependecies
// Tryn'g to add protobuf dependencies
JavaArchive[] prto_buf = Maven.configureResolver().withMavenCentralRepo(true).resolve("com.google.protobuf:protobuf-java:2.3.0").withTransitivity().as(JavaArchive.class);
for (int i = 0; i < prto_buf.length ; i++) {
projectPackages = projectPackages.merge(prto_buf[i]);
}
Throws sameException...
Again , with a simple
System.out.println(merge.toString(true));
I can see that com.Google.protobuf.GeneratedMessage is present
Extract of dependencies:
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-depchain</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.5.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
Can anybody help me solve this issue?
This smells to me like a classloading issue. Especially considering the fact that you are using embedded container which basically takes everything what's in there from your project classpath.
What is your target container you are deploying your application on? Maybe if you try to run your test there instead of embedded container you will at least see if that is really the problem, or there is still something wrong with your deployment artifact.
Just taking Spring-Boot for a spin and decided to mix in Camel because I need some arcane Headers work in the rest client I am working on. Setting up the application was fine until I added the camel-http component to my POM, then I get this on init:
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
I've havent got the first idea of where to start to look for the problem. I gather Spring Boot will look up the classpath and try to wire stuff up, so is there a way for the to block the Camel packages from being acted on or something of the sort?
Complete log of the start up in this Gist
Here's my main aplication code:
#ComponentScan
#EnableAutoConfiguration
public class Application {
private static ApplicationContext ctx;
public static void main(String[] args) throws Exception {
ctx = SpringApplication.run(Application.class, args);
//Right outta Spring 4 docs
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
//---
// FIXME: ugly hack to allow some POC woek while wait for proper Camel/Spring4 unit tests fix.
Application app = new Application();
app.executeTests();
}
/**
* Dev QOL - unit tests are broken for now, see:
* https://issues.apache.org/jira/browse/CAMEL-7074
* <p/>
* Waiting for fix (Too lay to checkout and build my own Camel)
*/
private void executeTests() throws Exception {
testAuth();
}
#Bean
DefaultCamelContext camelCtx() throws Exception {
DefaultCamelContext camel = new DefaultCamelContext();
camel.addRoutes(cryptsyRouteBuilder());
camel.start();
return camel;
}
#Bean
public CryptsyRouteBuilder cryptsyRouteBuilder() throws Exception{
CryptsyRouteBuilder bean = new CryptsyRouteBuilder();
bean.setCryptsy(cryptsy());
return bean;
}
#Bean
public Cryptsy cryptsy() throws IOException {
return new Cryptsy();
}
protected void testAuth() throws Exception {
ProducerTemplate producer = camelCtx().createProducerTemplate();
producer.requestBody("direct:start", "Why, hullo there", String.class);
}
}
And my POM dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-actuator</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-quartz</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>${camel.version}</version>
</dependency>
<!-- Assorted -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<properties>
<start-class>xxx.xxxx.Application</start-class>
<camel.version>2.12.2</camel.version>
</properties>
The exception is telling you that Spring Boot thinks you want to build a web server, but can't find the right dependencies on the classpath. The most obvious reason for that in your case would be that the HTTP dependencies you added included Servlet APIs. I see no reason why you need that for a client app, but only you would know whether you need it or not. Maybe you can exclude it?
If you do need the Servlet dependencies and just want to explicitly tell Boot that you aren't creating a web application you can set the property spring.main.web_environment=false, or use the SpringApplication (or SpringApplicationBuilder) API directly to set the same flag. See docs here for background information.