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
Related
i'm building my first SOAP application using Spring Boot + Apache CXF and Maven.
I've followed this guide:
https://www.baeldung.com/apache-cxf-with-spring
Without using XML file, so at the end i have those classes:
DemoApplication
#SpringBootApplication
public class DemoApplication{
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
ServiceConfiguration
#Configuration
public class ServiceConfiguration {
#Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
ServletDestinationFactory destinationFactory = new ServletDestinationFactory();
SpringBus bus = new SpringBus();
bus.setExtension(destinationFactory, HttpDestinationFactory.class);
return bus;
}
#Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), new BaeldungImpl());
endpoint.publish("http://localhost:8080/demo-0.0.1-SNAPSHOT");
return endpoint;
}
}
AppInitializer
public class AppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ServiceConfiguration.class);
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new CXFServlet());
dispatcher.addMapping("/services/*");
}
}
BaeldungImpl
#WebService(endpointInterface = "com.example.demo.Baeldung")
public class BaeldungImpl implements Baeldung {
private int counter;
#Override
public String hello(String name) {
return "Hello " + name + "!";
}
#Override
public String register(Student student) {
counter++;
return student.getName() + " is registered student number " + counter;
}
#Override
public String ProvaServizio() {
return "";
}
}
Baeldung
#WebService
public interface Baeldung {
public String hello(String name);
public String register(Student student);
public String ProvaServizio();
}
Student
public class Student {
private String name;
Student() {
}
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
After buildin and deploying on Tomcat, following this URL i reach this:
Available SOAP services:
If i try to go further (adding /hello,/hegister or /ProvaServizio) i've got this error:
No service was found.
And the log says:
15:48:19.416 [http-nio-8080-exec-127] WARN org.apache.cxf.transport.servlet.ServletController - Can't find the request for http://localhost:8080/demo-0.0.1-SNAPSHOT/services/hello's Observer
Here is my 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 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.4.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</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</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>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm building a small desktop app using JavaFX and Spring Boot. I used JavaFX Weaver to integrate JavaFX and Spring Boot together.
I want to use the JPA repository Interface to access a MySQL database, however, I get an error when starting the app because of the auto wiring of the repository field.
Console Error
Project Structure
Service
#Service
public class ColorService {
// I get the error when I add these 2 lines of code
#Autowired
private ColorRepository colorRepository;
public List<Color> getColors(){
return colorRepository.findAll();
}
}
Repository
#Repository
public interface ColorRepository extends JpaRepository<Color, Integer> {
}
Entity
#Entity
#Table(name="color")
public class Color {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="color_id")
private int id;
#Column
private String colorName;
public Color() {
}
public Color(String colorName) {
this.colorName = colorName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getColorName() {
return colorName;
}
public void setColorName(String colorName) {
this.colorName = colorName;
}
}
Main App
#SpringBootApplication
public class MainApp {
public static void main(String[] args) {
Application.launch(JavaFxApplication.class, args);
}
}
JavaFX Main App
public class JavaFxApplication extends Application {
private ConfigurableApplicationContext applicationContext;
#Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
this.applicationContext = new SpringApplicationBuilder().sources(MainApp.class).run(args);
}
#Override
public void start(Stage stage) {
FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
Parent root = fxWeaver.loadView(MainController.class);
Scene scene = new Scene(root);
scene.getStylesheets().add("css/style.css");
stage.setScene(scene);
stage.show();
}
#Override
public void stop() {
this.applicationContext.close();
Platform.exit();
}
}
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 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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.diogo</groupId>
<artifactId>habittracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>habittracker</name>
<description>Habit Tracker</description>
<properties>
<java.version>11</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>net.rgielen</groupId>
<artifactId>javafx-weaver-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>com.diogo.habittracker.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
The package of your class ColorRepository is not defined in component scan.
try to add : #ComponentScan({"package1","package2","..."})
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 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")