Question: Do you have maven?
If not I would encourage you to download it. It's simple: https://maven.apache.org/
If yes, add these to your pom file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Now in the class which has your main application add some annotations and a controller (method) to handle your POST request:
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Controller
@EnableAutoConfiguration
public class SpringBootSampleApplication {
@RequestMapping("/")
@ResponseBody
@CrossOrigin
String home() {
return "Hello World!";
}
/** Receives a POST request on path / with parameter name. Will return parameter
*
* @param request
* @return */
@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody
@CrossOrigin // this will allow requests origination from any domain. see CORS
String greet(HttpServletRequest request) {
return "Greetings: " + request.getParameter("name");
}
/** starts the application
*
* @param args
* @throws Exception */
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootSampleApplication.class, args);
}
}
This will start a web server on port 8080.
Check out this code sample here: https://github.com/mikibrv/spring.boot.sample
I have also added a small index.html file in the resources as an example of AJAX.
It's a full project, generating 2 endpoints ( a GET and POST ) with CORS enabled and when you build it with Maven (mvn clean install) you get a fat JAR which you can call using : java -jar target/spring.boot.sample.jar
For more info: http://projects.spring.io/spring-boot/