I am trying to create a simple REST project with Maven in Eclipse.
I have created a new Dynamic Webapp Project and than right click --> configure/ convert Maven Project.
Now If I create a simple index.html with only one h1, it works.
So I created the GreetingController.java:
package resources;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
#RequestMapping("/greeting")
public Greeting greeting(#RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
and the model: Greeting.java
package resources;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
and the 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.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<packaging>war</packaging>
</project>
If I go to http://localhost:7001/demo/greeting , it doesn't work (404). Same problem with http://localhost:7001/greeting .
I think the problem is that the server doesn't map the annotation.
The RequestMapping value should not include the context root. Change the value to
#RequestMapping("/greeting")
Related
I have just started a Spring tutorial. I do everything the same as the lecturer, but if I make a getRequest nothing changes. I also do not have any grey globe icons next to the #RequestMapping and #GetMapping annotations ss of grey globe icon. Would be happy to have any suggestions. Here is the erorr I get when I call the controller endpoint error page
package Controller;
import model.Il;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
#RestController
#RequestMapping("/iller")
public class ILController {
#GetMapping
public ResponseEntity<List<Il>> getIller(){
Il il1 = new Il("34", "Istanbul");
Il il2 = new Il("06", "Ankara");
List<Il> iller = Arrays.asList(il1, il2);
return new ResponseEntity<>(iller, HttpStatus.OK);
}
}
Il class:
package model;
import lombok.Data;
#Data
public class Il {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Il(){
this.id = "defaultId";
this.name = "defaultName";
}
public Il(String id, String name){
this.id = id;
this.name = name;
}
}
I built your example from scratch with Intellij. Could you try re-editing your code as in the screenshot? If you need help with Spring, please write.
you need to fill the annotation #GetMapping with a path that defines your GET method.
For example, try something like this:
#GetMapping(value = "/GetIller")
public ResponseEntity<List<Il>> getIller(){
Il il1 = new Il("34", "Istanbul");
Il il2 = new Il("06", "Ankara");
List<Il> iller = Arrays.asList(il1, il2);
return new ResponseEntity<>(iller, HttpStatus.OK);
}
Same for other HTTP methods (POST, PUT, PATCH, DELETE ...)
Please check the pom also:
<?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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 9090
Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Regarding the Glob Icon feature, I think that feature is only available in the IntelliJ Enterprise version.
By clicking that grey globe icon it gives 3 options -
Go to declaration or usage
Generate request in HTTP Client
Show all endpoints of module
As is clear from the topic of the question, this is my stack of technologies. I have a problem encoding Russian characters.
Its my controller:
#RestController
public class ProductController {
#Autowired
private ProductService productService;
#RequestMapping("/Product/ByQuery")
public List getProductsByQuery(#RequestHeader(name = "query" , required = false) String query) {
System.out.println(new String(query.getBytes(),StandardCharsets.ISO_8859_1));
return productService.findByQuery(new String(query.getBytes(),StandardCharsets.ISO_8859_1));
}
}
its my Service :
public class ProductServiceImpl implements ProductService {
#Autowired
private ProductRepository productRepository;
#Override
public List<Product> findByQuery(String query) {
List<DataModel.Product> productList = productRepository.findDistinctByNameContainingIgnoreCaseOrCategoryContainingIgnoreCaseOrManufacturerContainingIgnoreCaseOrInformationValueContainingIgnoreCase(query,query,query,query);
List<Product> resultProducts = new ArrayList<>();
ModelMapper modelMapper = new ModelMapper();
for (int i = 0; i < productList.size(); i++) {
int like = 0;
int dislike = 0;
for (Rating rating : productList.get(i).getRating()) {
if (rating.getType().equals("Like")) {
like += 1;
} else {
dislike += 1;
}
}
resultProducts.add(modelMapper.map(productList.get(i), Product.class));
resultProducts.get(i).setCommentCnt(productList.get(i).getComment().size());
resultProducts.get(i).setLike(like);
resultProducts.get(i).setDislike(dislike);
resultProducts.get(i).setImage(productList.get(i).getImage().get(0).getUrl());
}
return resultProducts;
}
}
Repository:
#Repository
public interface ProductRepository extends JpaRepository<Product,Long> {
List<Product> findDistinctByNameContainingIgnoreCaseOrCategoryContainingIgnoreCaseOrManufacturerContainingIgnoreCaseOrInformationValueContainingIgnoreCase(String name,String category, String manufacturer, String value);
}
Application :
#SpringBootApplication(scanBasePackages = {"Service", "repository", "controller"})
#EntityScan("DataModel")
#EnableJpaRepositories(basePackages = "repository")
public class CloudliquidApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(CloudliquidApplication.class, args);
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);
}
}
#Bean
public ProductService productService() {
return new ProductServiceImpl();
}
#Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
#Override
protected void postProcessContext(Context context) {
((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
}
};
}
}
Property :
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
pom:
<?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>com.lopamoko</groupId>
<artifactId>cloudliquid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cloudliquid</name>
<description>Demo project for Spring Boot</description>
<pluginRepositories>
<pluginRepository>
<id>maven-annotation-plugin-repo</id>
<url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
What do I need to do, so that Russian characters will finally begin to be perceived?
Before moving to the spring, it helped me - new String (source.getBytes (), StandardCharsets.UTF-8)). But after the move it stopped working (in any format).
What else should you try? If anything, I transfer the data from Android (Retrofit).
move String query from #RequestHeader to #RequestParam
I am currently following a Pluralsight tutorial about Java Microservices with Spring Cloud Security. After I run this application, I copy the generated security password from log and in Postman i am calling
http://localhost:9001/services/tolldata using Basic Auth with user: user and password: the generated one. But it gives as response 404 Not Found.
I have tried to change port from application settings, the same result.
Tried to add also the host address in properties, the same result.
Deleted the server.servlet.context-path, same result.
Placed the #RequestMapping("/tolldata") above the getTollData() method, same result.
This is my main class.
#SpringBootApplication
#RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#RequestMapping("/tolldata")
public class TollUsage{
public String id;
public String stationId;
public String licensePlate;
public String timestamp;
public List<TollUsage> getTollData(){
TollUsage instance1 = new TollUsage("100", "station150", "B65GT1W", "2016-09-30T06:31:22");
TollUsage instance2 = new TollUsage("101", "station119", "AHY673B", "2016-09-30T06:32:50");
TollUsage instance3 = new TollUsage("102", "station150", "ZN2GP0", "2016-09-30T06:37:01");
List<TollUsage> tolls = new ArrayList<TollUsage>();
tolls.add(instance1);
tolls.add(instance2);
tolls.add(instance3);
return tolls;
}
public TollUsage(){}
public TollUsage(String id, String stationId, String licensePlate, String timestamp){
this.id = id;
this.stationId = stationId;
this.licensePlate = licensePlate;
this.timestamp = timestamp;
}
}
}
This is my application.properties
server.port=9001
server.servlet.context-path=/services
And finally the 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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Put #RestController on your TollUsage class, not DemoApplication and split these classes to diffrent files.
Try this:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
#RequestMapping(value = "/tolldata")
#RestController
class TollUsage{
public String id;
public String stationId;
public String licensePlate;
public String timestamp;
#GetMapping
#CrossOrigin
#ResponseBody
public ResponseEntity<List<TollUsage>> getTollData(){
TollUsage instance1 = new TollUsage("100", "station150", "B65GT1W", "2016-09-30T06:31:22");
TollUsage instance2 = new TollUsage("101", "station119", "AHY673B", "2016-09-30T06:32:50");
TollUsage instance3 = new TollUsage("102", "station150", "ZN2GP0", "2016-09-30T06:37:01");
List<TollUsage> tolls = new ArrayList<TollUsage>();
tolls.add(instance1);
tolls.add(instance2);
tolls.add(instance3);
return new ResponseEntity<>(tolls, HttpStatus.OK);;
}
public TollUsage(){}
public TollUsage(String id, String stationId, String licensePlate, String timestamp){
this.id = id;
this.stationId = stationId;
this.licensePlate = licensePlate;
this.timestamp = timestamp;
}
}
I've been trying to use Maven to transform a RESTful service to a war file and deploy it to Tomcat.
But all I received is a 404 not found.
These are my code:
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
This is Greeting.java:
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
}
This is my controller:
#RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
#RequestMapping("/greeting")
public Greeting greeting(#RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}
This 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.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
</dependencies>
<properties>
<java.version>1.7</java.version>
<start-class>gs-rest-service-master.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
<packaging>war</packaging>
The rest service is just fun because I can run it using jar.
But when I move the war file to /webapp and run localhost:8080/rest/greeting,
It failed.Could you help me?
(rest is the name of the rest service,greeting is the function)
Your Application class is missing few annotations
#Configuration
#ComponentScan
#SpringBootApplication
#EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
And also make sure your pom file has the following dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Also add public getters to Greetings class in order to avoid error 406, "Could not find acceptable representation"
public class Greeting
{
private final long id;
private final String content;
public Greeting(long id, String content)
{
this.id = id;
this.content = content;
}
/**
* #return the id
*/
public long getId()
{
return id;
}
/**
* #return the content
*/
public String getContent()
{
return content;
}
}
Spring boot will make it build a web application, only when
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
is added. You need to exclude embedded tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Refer Doc https://spring.io/blog/2014/03/07/deploying-spring-boot-applications
I'm working through a Spring tutorial at http://spring.io/guides/gs/validating-form-input/, and get a 404 upon attempting to access the localhost root.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Aug 06 02:36:15 CDT 2014
There was an unexpected error (type=Not Found, status=404).
I modified the workflow a bit to use Eclipse's Maven project settings. The steps I took, that were different from the tutorial, in order:
Created a Maven Project in Eclipse
Replaced the pom.xml that was generated with the one from the tutorial
The tutorial instruct to have the template HTML files in resources/templates, and Spring threw this exception (quite a few internal exceptions, actually), Caused by: java.lang.IllegalStateException: Cannot find template location: class path resource [templates/] (please add some templates or check your Thymeleaf configuration)
To resolve, I moved the templates form.html and results.html to /templates on the classpath.
I checked with the debugger the WebController class, and none of it's code is being hit. I don't believe it is a local configuration issue, because the completed tutorial in their git repo behaves as expected.
If anyone has any insight at all, it would be much appreciated. Thank you!
These are the files from my non-working attempt.
WebController.java
package hello;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
public class WebController extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/results").setViewName("results");
}
#RequestMapping(value="/", method=RequestMethod.GET)
public String showForm(Person person) {
return "form";
}
#RequestMapping(value="/", method=RequestMethod.POST)
public String checkPersonInfo(#Valid Person person, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "form";
}
return "redirect:/results";
}
}
Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Person.java
package hello;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Person {
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person [name=")
.append(name)
.append(", age=")
.append(age)
.append("]");
return builder.toString();
}
#Size(min=2, max=30)
private String name;
#NotNull
#Min(18)
private Integer age;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
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.springframework</groupId>
<artifactId>gs-validating-form-input</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.4.RELEASE</version>
</parent>
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>hello.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
There is no #Controller on your controller. As far as Spring is concerned this class is just a simple pojo that it shouldn't care about: it's not defined anywhere and it's not picked up by classpath scanning since it does not have the proper annotation on it.