Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 678 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating spring rest services without using spring boot

#1
I've followed the Getting Started tutorial on spring.io for building REEST services

[To see links please register here]

. The problem is that this tutorial only explain how to produce a standalone running jar with tomcat embedded using spring boot.

Is there a way to create a project from scratch to produce a war to deploy for instance on an already existing tomcat instance?

PS: I had found a previous thread

[To see links please register here]

on stackoverflow concerning the very same issue. The problem is that the accepted answers and suggestions doesn't exactly solve my problem, since I'm not looking for ways to modify the standalone-app spring boot project so that it works on an external tomcat container, but would like to find a 'cleaner' solution not involving spring boot at all. (I'm not exactly sure how to behave here, being still quite new at stackoverflow. I hope that opening a new question is the correct procedure).

Reply

#2
You don't need Spring Boot to create a rest controller.

Please follow the spring framework documentation on how to setup MVC

[To see links please register here]


The MVC setup (the `DispatcherServlet`) depends on your spring version, you can either use xml or you can setup programmatically:

[To see links please register here]


Once this is setup, you can add a rest controller to your application. Note that a rest controller (the `@RestController` annotation) is a stereotype annotation that combines `@ResponseBody` and `@Controller`, in other words the Controller returns an object in the response body instead of returning a view.

This is a perfect example explaining what I said above:

[To see links please register here]



Reply

#3
Here is another example:

Directory Layout:

.
├── ./pom.xml
└── ./src
└── ./src/main
├── ./src/main/java
│   └── ./src/main/java/biz
│   └── ./src/main/java/biz/tugay
│   └── ./src/main/java/biz/tugay/restfulspring
│   └── ./src/main/java/biz/tugay/restfulspring/config
│   ├── ./src/main/java/biz/tugay/restfulspring/config/RestfulHello.java
│   └── ./src/main/java/biz/tugay/restfulspring/config/WebAppInitalizer.java
└── ./src/main/webapp
└── ./src/main/webapp/WEB-INF
└── ./src/main/webapp/WEB-INF/web.xml

**pom.xml**

<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

[To see links please register here]

;

<modelVersion>4.0.0</modelVersion>
<groupId>biz.tugay</groupId>
<artifactId>restfulSpring</artifactId>

<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>restfulSpring Maven Webapp</name>

<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
</dependencies>

<build>
<finalName>restfulSpring</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
</plugin>
</plugins>
</build>

</project>

**web.xml**

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

[To see links please register here]

;
version="3.0">
</web-app>

**WebAppInitalizer.java**

package biz.tugay.restfulspring.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

@Configuration
@EnableWebMvc
@ComponentScan("biz.tugay.restfulspring")
public class WebAppInitalizer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{WebAppInitalizer.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}
}

**RestfulHello.java**

package biz.tugay.restfulspring.config;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/")
public class RestfulHello {

@RequestMapping(value = "hello")
public ResponseEntity<String> sayHello() {
final HttpHeaders httpHeaders= new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<String>("{\"msg\": \"Hello World\"}", httpHeaders, HttpStatus.OK);
}
}

Build and run:

mvn clean install
mvn jetty:start

Test:

> GET /hello HTTP/1.1
> Host: localhost:8080
> User-Agent: insomnia/5.15.0
> Accept: */*
< HTTP/1.1 200 OK
< Date: Fri, 27 Apr 2018 00:06:07 GMT
< Content-Type: application/json
< Content-Length: 22
< Server: Jetty(9.2.1.v20140609)

Content received:

{
"msg": "Hello World"
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through