I have a simple Spring Boot application that serves some static resources when accessed from localhost:8899.
I installed tomcat on my pc and tried to deploy the war file, but it kept giving me a 404 response. Next, I wanted to modify some things in the spring boot app, but now if I start up the app, I can't reach it.
I also tried to use Postman to see if at least the rest controller is working, but I get the "There was an error connecting to localhost:8899" message.
I ended up uninstalling the tomcat service, I also uninstalled tomcat and reverted to a previous commit on the application, which was working before, but I get the same result. The app starts up at port 8899 without errors, but I can't get any response neither by accessing the static resources from the browser nor from sending a request from Postman to the rest controller.
I have no clue what to look for because I get no errors other than what you can see in the screenshot.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>todoey-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>todoey-backend</name>
<description>Reminder application</description>
<properties>
<java.version>1.10</java.version>
</properties>
<parent>
<groupId>org.reminder</groupId>
<artifactId>todoey-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8899
# DB
spring.datasource.url=jdbc:h2:file:./todoey-be/src/main/resources/db/todoey_db
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
ReminderController.java
package com.reminder.todoey.controller;
import com.reminder.todoey.model.Reminder;
import com.reminder.todoey.service.ReminderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
#RestController
#RequestMapping("reminder")
public class ReminderController {
#Autowired
private ReminderService reminderService;
#GetMapping
public ResponseEntity<List<Reminder>> getAllReminders() {
List<Reminder> reminderList = reminderService.getAllReminders();
return ResponseEntity.ok(reminderList);
}
#PostMapping
public ResponseEntity writeReminder(#RequestBody final Reminder reminder) {
reminderService.saveReminder(reminder);
return ResponseEntity.status(HttpStatus.OK).build();
}
#DeleteMapping
public ResponseEntity deleteReminder(#RequestParam final long id) {
reminderService.deleteReminder(id);
return ResponseEntity.status(HttpStatus.OK).build();
}
#PutMapping
public ResponseEntity updateReminder(#RequestBody final Reminder reminder) {
reminderService.updateReminder(reminder);
return ResponseEntity.status(HttpStatus.OK).build();
}
#PutMapping(path = "/email-address")
public ResponseEntity updateEmailAddress(#RequestParam final String email) {
reminderService.updateEmailAddress(email);
return ResponseEntity.status(HttpStatus.OK).build();
}
#GetMapping(path = "/email-address")
public ResponseEntity<String> getEmailAddress() {
return ResponseEntity.ok(reminderService.getEmailAddress());
}
}
I managed to figure out why I couldn't reach my application. I had an async method with and infinite loop that was checking the time to see when to send the reminders. This method was annotated with both #Async and #PostConstruct because I wanted it to be called as soon as the application starts. After deleting #PostConstruct, everything worked just fine.
Related
I am trying to debug why a my application won't show, and in doing so I have this small application running hello world. This does work, it shows the text on the screen, great. But then if I add spring-boot-starter-data-jpa to the pom file, I get a 404 error instead. I need to use spring-boot-starter-data-jpa and have no idea why it doesn't work as expected.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-hello</artifactId>
<packaging>war</packaging>
<name>Spring Boot Hello World Example</name>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-tomcat</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!--this is the issued one-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>testapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller
package org.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#RequestMapping("/")
String hello() {
return "Hello World, Spring Boot!";
}
}
main class
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class MyWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyWebApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(MyWebApplication.class);
}
}
I have gone through every other answer I could find and made sure I was using restcontroller instead of controller, I believe my main class is in the correct spot, I am at a loss as to why one dependency, especially one that isn't being used yet, causes a 404.
In application.properties file in resource folder did you set database configuration such asusername, password and url? It should be set like this.
spring.datasource.url=jdbc:mysql://localhost:3306/restapi
spring.datasource.username=root
spring.datasource.password=
And it's better to use #GetMapping annotation instead of #RequestMapping.
Error: I get the following in Firefox: Foreign Site Query Blocked: The same origin policy does not allow reading of the remote resource http: // localhost: 8080 / api / v1 / post /. (Cause: The 'Access-Control-Allow-Origin' CORS header does not exist)
I have spent several hours to allow CORS communication with my Spring Boot server in order to make my REACT UI communicate with the server. There are lot of similarly phrased questions on Stack Overflow, but non of the suggested solutions have solved my problem...
Spring Boot suggests different solutions on https://spring.io/guides/gs/rest-service-cors/. One local solution is using annotations. A global solution can be obtained with a config file.
I have tried annotating the controller with #CrossOrigin as seen in my code below:
package com.example.Blogging.api;
import com.example.Blogging.model.Post;
import com.example.Blogging.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
#CrossOrigin(origins = "http://localhost:3000")
#RequestMapping("api/v1/post")
#RestController
public class PostController {
private final PostService postService;
#Autowired
public PostController(PostService postService) {
this.postService = postService;
}
#PostMapping
public void addPost(#RequestBody Post post){
postService.addPost(post);
}
#GetMapping
public List<Post> getAllPosts(){
return postService.getAllPosts();
}
#GetMapping(path = "{id}")
public Optional<Post> getPostById(#PathVariable("id") UUID id){
return postService.getPostById(id);
}
#PutMapping(path="{id}")
public void updatePostById(#PathVariable("id") UUID id, #RequestBody Post post){
postService.updatePost(id,post);
}
#DeleteMapping(path="{id}")
public void deletePostById(#PathVariable("id") UUID id){
postService.deletePost(id);
}
}
This did not work.. I have also tried annotating on each method instead of the whole controller class.
Furthermore I have tried to make a config file:
package com.example.Blogging.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*");
}
}
Nothing really seems to work. The console in my browser keeps saying 'access-control-allow-origin' is missing in the response header..
For reference, I will also put my pom.xml below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Blogging</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Blogging</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SPRING SECURITY -->
<!--<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<scope>test</scope>
</dependency>-->
<!-- SPRING BOOT STARTER SECURITY -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
</plugins>
</build>
</project>
Anyone who can suggest a solution? Thank you in advance!
I just noticed, you have Spring Security in your dependencies.
You might missed to enable CORS in your WebSecurityConfig.
Eg. something like this:
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()...
}
}
I try to create a Spring Boot REST application with GraphQL and MongoeDB (I start from that article https://medium.com/oril/spring-boot-graphql-mongodb-8733002b728a). The application start but I got 404 error when I try to post something to a endpoint.
I read also that #PostConstruct is not supported anymore with 2.2.0 (it's another problem I'll try to figure out later, I try to preload the database during startup).
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tsoai-api</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<lombok.version>1.18.10</lombok.version>
<commons-io.version>2.5</commons-io.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My main application file look like this (nothing very special!):
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
MainController.java
#RestController
#RequestMapping(path = "/query")
public class MainController {
private GraphQL graphQL;
private GraphQlUtility graphQlUtility;
#Autowired
MainController(GraphQlUtility graphQlUtility) throws IOException {
this.graphQlUtility = graphQlUtility;
graphQL = graphQlUtility.createGraphQlObject();
}
#PostMapping(path= "/")
public ResponseEntity query(#RequestBody String query){
ExecutionResult result = graphQL.execute(query);
System.out.println("errors: "+result.getErrors());
return ResponseEntity.ok(result.getData());
}
}
When I post on http://localhost:8080/query, I got this error:
{
"timestamp": "2019-10-19T16:10:19.136+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/query"
}
It will match /query/ only but not /query . If you want to match both /query and /query/ , you can just leave path of the #PostMapping in the query method as the default.
#RestController
#RequestMapping(path = "/query")
public class MainController {
#PostMapping
public ResponseEntity query(#RequestBody String query){
}
}
There could be a reason for this error that Spring boot is not scanning this class. Please try to add ComponentScan annotation on Spring boot main class with the package where you keep your controllers as given below.
#ComponentScan(basePackages= {"com.example"})
Check the port on which your Spring Server is running, According to the medium post they have configured it to run on :8000 while you're hitting on :8080.
The error states the same that it cannot find any controller mapped to this api-endpoint.
http://localhost:8000/query/
`
I have a web application using maven & spring boot with a war deployment.
While it works normally on a windows machine (starting it using netbeans), I get a blank page response from browser when I run it on a linux machine (not sure if this is relevant). No errors occur on startup.
After searching a while I found that it is related with some 404 error response of spring boot. This happens for any route that I try (valid or not)
Another clue is that I tried to redirect 404 errors to a test.jsp but nothing changed in the browsers (I still get this blank page). With postman I get this .jsp as a response.
In any case it's not normal to get an error response since the routes are correct.
Here is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=`"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">`
<modelVersion>4.0.0</modelVersion>
<groupId>org</groupId>
<artifactId>app</artifactId>
<version>1.0-RELEASE</version>
<packaging>war</packaging>
<name>webapp</name>
<description></description>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!--<failOnMissingWebXml>true</failOnMissingWebXml>-->
<webResources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Any ideas?
EDIT
I add some extra info that might help:
No MVC-relevant configuration is included in application.properties
Application.java (I'm not using #EnableWebMvc):
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Web MVC Configuration is done by:
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver getPageViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
resolver.setOrder(1);
return resolver;
}
}
After some tries I conclude that:
mvn spring-boot:run
java -classpath "lib/*:classes/." org.app.Application
java -jar webapp-1.0.RELEASE.war
start the app as a normal application which leads to this problem. No matter if pom packing is defined as jar or war (If I'm wrong someone pls correct me)
Netbeans was deploying the app into an external tomcat server. I finally tried the classic war deployment way in order to make it work.
This answer is not solving the original question but provides a fix in case someone else has the same problem.
I m trying to do Spring MVC tutorial:
It makes you create a main controller ad start a SpringApplication.
When I run the SpringApplication, I can see that a Tomcat server gets started, a controller class gets instanced as it should.
However, the mapping seems to fail : #RequestMapping("/greeting")
When I try to browse http://localhost:8080/TestSpringOpenEMM2/greeting
or http://localhost:8080/TestSpringOpenEMM2/greeting , I always get a 404 error.
(TestSpringOpenEMM2 is my project name).
I use eclipse IDE.
Here are my files:
package application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
System.out.println("App starting" );
SpringApplication.run(Application.class, args);
System.out.println("App started" );
}
}
controller:
import javax.servlet.annotation.WebServlet;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class GreetingController {
static{
System.out.println("Static init GreetingController");
}
#RequestMapping("/greeting")
public String greeting(#RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
System.out.println("starting servlet (greeting)");
model.addAttribute("name", name);
return "greeting";
}
public GreetingController(){
super();
System.out.println("new GreetingController");
}
}
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestSpringOpenEMM2</groupId>
<artifactId>TestSpringOpenEMM2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
</project>
Maybe you can use #RestController not #Controller. For REST api, if I use #Controller, the request will cause a 404 not found error, but after I change it to #RestController, the request works fine.
If you raise the Spring log level it should, when web-app starts, show you which URLs are being mapped to which classes/methods. That may help.
Also, if you're using Eclipse, right click project -> Properties -> Web Project Settings -> Context root. Make sure it is as you expect it to be i.e. TestSpringOpenEMM2. I have seen plenty of examples of this (context root) not being as expected in Eclipse.
The spring-boot-starter-parent is a great way to use Spring Boot, hence use a pom.xml like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestSpringOpenEMM2</groupId>
<artifactId>TestSpringOpenEMM2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
By default spring boot using a embedded tomcat container, you just need to run the main method. Read more on this topic on Spring Boot's Doc