#Pathvariable not found compilation error [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Im trying to execute a Spring MVC project using Maven but get a compilation error while maven packaging -
Error
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project CounterWebApp: Compilation failure
[ERROR] /home/prem1980/apache-maven/all_maven_projects/java_webapp_project/CounterWebApp/src/main/java/com/mkyong/controller/BaseController.java:[23,36] cannot find symbol
[ERROR] symbol : class PathVariable
[ERROR] location: class com.mkyong.controller.BaseController
java file
package com.mkyong.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/")
public class BaseController {
#RequestMapping(value="/welcome", method = RequestMethod.GET)
public String welcome(ModelMap model) {
model.addAttribute("message", "Maven Web Project + Spring 3 MVC - welcome()");
//Spring uses InternalResourceViewResolver and return back index.jsp
return "index";
}
#RequestMapping(value="/welcome/{name}", method = RequestMethod.GET)
public String welcomeName(#PathVariable String name, ModelMap model) {
model.addAttribute("message", "Maven Web Project + Spring 3 MVC - " + name);
return "index";
}
}
Project structure
[pr#web449 CounterWebApp]$ tree .
.
├── pom.xml
├── src
│   └── main
│   ├── java
│   │   └── com
│   │   └── mkyong
│   │   └── controller
│   │   └── BaseController.java
│   ├── resources
│   └── webapp
│   └── WEB-INF
│   ├── index.jsp
│   ├── mvc-dispatcher-servlet.xml
│   └── web.xml
└── target
├── classes
├── generated-sources
│   └── annotations
└── maven-status
└── maven-compiler-plugin
└── compile
└── default-compile
└── createdFiles.lst

Add the import statement
import org.springframework.web.bind.annotation.PathVariable;

I think you are missing the spring-web jar in your classpath. The spring-web jar contains that annotation.
Ensure that your pom.xml contains:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
Ensure you have spring-web jar.
If you check where org.springframework.web.bind.annotation.PathVariable is located you will find that it is within above jar:
http://mvnrepository.com/artifact/org.springframework/spring-web/3.0.4.RELEASE
Of course the jar version may be different, just ensure to use yours. You can find spring-web versions here:
http://mvnrepository.com/artifact/org.springframework/spring-web
And as Reimeus pointed in his answer, you need the import as well.

Related

Annotation proccessor configuration from a Gradle multi module build in IntelliJ 2019.3 seems not to work correctly

I have a Gradle multi module project that uses the Mapstruct annotation processor for data type mapping across Java modules. The Gradle build works fine but when I import the project into IntellJ IDEA 2019.3 I get an unexpected annotation processor configuration.
The project structure looks like this
.
├── build.gradle
├── module1
│   └── src
│   └── main
│   └── java
│   └── io
│   └── wangler
│   └── mapstruct
│   ├── ApplicationModule1.java
│   ├── Person.java
│   ├── PersonDTO.java
│   └── PersonMapper.java
├── module2
│   └── src
│   └── main
│   ├── generated
│   │   └── ch
│   │   └── silviowangler
│   │   └── mapstruct
│   │   └── CarMapperImpl.java
│   └── java
│   └── ch
│   └── silviowangler
│   └── mapstruct
│   ├── ApplicationModule2.java
│   ├── Car.java
│   ├── CarDTO.java
│   └── CarMapper.java
└── settings.gradle
and the build.gradle that registers the annotation processor for module1 and module2.
subprojects { p ->
apply plugin: 'java-library'
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
testImplementation 'org.codehaus.groovy:groovy-all:2.5.8'
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
testImplementation 'junit:junit:4.12'
}
}
When I compile the project using ./gradlew compileJava all works out fine and I get no compilation errors.
But when I run Rebuild Project withing IntelliJ I get a compilation error in module1 since IntelliJ does not have an annotation processor registered for module1.
Error:(6, 35) java: cannot find symbol
symbol: class PersonMapperImpl
location: class io.wangler.mapstruct.ApplicationModule1
Am I doing something wrong here or is this a known IntelliJ issue? The source code for this example can be found at https://github.com/saw303/idea-annotation-processors
I faced the same issue in IDEA 2019.3. Looks like a bug.
It occurs only if two modules has the same set of annotation processors.
To solve the problem you need to add any library using annotationProcessor directive to one of modules. It does not have to be a real annotation processor. This one is working for me:
annotationProcessor "commons-io:commons-io:${commonsIoVersion}"
I have created a defect in JerBrains bugtracker: IDEA-230337

Multi-module Spring project: override a resource file in test

I have a Spring project with the following structure:
.
├── pom.xml
├── core-module
│   ├── pom.xml
│   └── src
│   ├── main
│   │   ├── java
│   │   └── resources
│   │   ├── applicationContext.xml
│   │   └── file.xml
│   └── test
│   ├── java
│   └── resources
│   ├── file.xml
│   └── foo.xml
└── web-module
├── pom.xml
└── src
├── main
│   └── java
└── test
└── java
In my applicationContext.xml file, I have the following import:
<import resource="classpath:file.xml" />
In production I want to use the file.xml defined in main, and during tests I want to use the one defined in the test folder; it's actually what is happenning when I run the tests of the core-module.
However, when I run the tests of the web-module, this is not the case, the file used is the one of the main folder.
How to perform what I want?
Some notes:
web-module has a test-jar dependency to core-module, so tests resources are included;
if I go inside the target directory, the file.xml is the good one in the test-classes folder;
when running web-module tests, if I put a breakpoint in the Spring code, then a getClass().getClassLoader().getResource() on file.xml returns the one defined in main, but if I run the same method on the foo.xml file it returns me the only one available in the test folder, so both files seems to be present, but it acts as if the main takes over the test resource...
Thank you!

Package does not exist IntelliJ

I'm using IntelliJ to develop a java application but I'm not able to import any of the packages I created. I marked my src folder as Sources Root and the tree suggests I'm able to import a package by just
import service.RMI;
on my App.java file.
but when I try to compile
javac App.java
I get an error saying
App.java:4: error: package service does not exist
import service.RMI;
^
Does anyone knows why this happens? Does it have to do with my project skeleton?
For a better understanding, I leave my project tree:
.
├── algorithms
│   └── SHA256.java
├── app
│   ├── App.class
│   └── App.java
├── file
│   ├── ChunkFile.java
│   └── Chunk.java
├── filesystem
│   └── filesystem.java
├── META-INF
│   └── MANIFEST.MF
├── peer
│   ├── listeners
│   │   ├── MClistener.java
│   │   └── MDBlistener.java
│   └── Peer.java
├── protocols
│   ├── Backup.java
│   ├── Delete.java
│   └── Restore.java
└── service
└── RMI.java
I found the solution.
I was compiling all wrong. I was trying to run
javac App.java
on my app directory but I figured out that I need to run
javac app/App.java
on my root directory - src, and then, to run the app I need
java app.App
Thanks for the kind replies.
Regards

Deploy Jersey Application to Tomcat - Resources 404

I have a tree that looks like this:
In the java folder, I have a package, com.example.project which has a Main class and an Application class that subclasses ResourceConfig
#ApplicationPath("api")
public class Application extends ResourceConfig {
public Application() {
packages("com.example.project");
property(MustacheMvcFeature.TEMPLATE_BASE_PATH, "templates");
register(MustacheMvcFeature.class);
register(MyRequestFilter.class);
}
}
In development, I run the Main class and start a Grizzly server:
public class Main {
public static HttpServer startServer(String BASE_URI) {
final Application app = new Application();
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), app);
}
public static void main(String[] args) throws IOException {
String BASE_URI = "http://localhost:8080/api";
final HttpServer server = startServer(BASE_URI);
server.getServerConfiguration().addHttpHandler(
new StaticHttpHandler("src/main/webapp"), "/");
System.out.println(String.format(
"Jersey app started with WADL available at "
+ "%s/application.wadl\nHit enter to stop it...", BASE_URI)
);
System.in.read();
}
}
Everything works fine with the Grizzly server. I'm now trying to deploy to tomcat.
I use mvn package to create a war file. In my pom file, I have:
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
I put the war file in the webapps directory under a tomcat instance and it gets exploded there. My static content is served.
The layout of the directory that comes from the WAR file looks like this:
[vagrant#vagrant-centos6 project]$ tree .
.
├── index.html
| ... <webapp content>
├── META-INF
│   ├── MANIFEST.MF
│   └── maven
│   └── com.example.project
│   └── project
│   ├── pom.properties
│   └── pom.xml
└── WEB-INF
├── classes
│   ├── com
│   │   └── example
│   │   └── project
│   │   ├── Application.class
│   │   ├── Main.class
| | ...
│   ├── filter-dev.properties
│   ├── filter-test.properties
│   ├── hibernate.cfg.xml
│   └── templates
│   └── foo.mustache
└── lib
...
I can not locate any of my REST services. Everything I try is 404.
Directory in tomcat webapps is project, javax.ws.rs.ApplicationPath is api, #Path is service. So, I would hope to get a response from /project/api/service, for instance. But, after trying many combinations, everything is 404.
You need to add jersey-servlet.jar to your war file.
In pom.xml, that might look like this:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>

import JPA Model from gradle sub-project in Spring

In an android project, I want to use a shared model library with the REST Server(JPA)
Project structure:
├── android
│ └ ...
│
├── model
│   ├── build.gradle
│   └── src
│   └── com
│   └── model // error at runtime
│   └── Customer.java
├── server
│   ├── build.gradle
│   └── src
│   └── com
│   └── server
│   ├── Application.java
│   ├── CustomerController.java
│   ├── CustomerRepository.java
│   └── model // works fine
│   └── Customer.java
├── build.gradle
I am using gradle sub-projects to manage the dependency which works fine at compile time.
But when I run the Application, Spring can't resolve the JPA annotations.
When I move the model class to the server project, it works fine.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [...] Instantiation of bean failed;
[...]
Not an managed type: class com.model.Customer
root build.gradle
buildscript { ... }
allprojects {
repositories {
mavenCentral()
}
}
project(":android") { ... }
project(":model") {
apply plugin: "java"
}
project(":server") {
apply plugin: "java"
dependencies {
compile project(":model")
}
}
Is there a solution to include JPA models from a gradle sub-project ?
The reason is mostly to prevent duplicate code between the android and server appication
You Customer entity will not be found by default because it is not in a sub-pacakge of your Application class. Try using the #EntityScan annotation (see http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-separate-entity-definitions-from-spring-configuration).

Categories