在不使用 spring 引导的情况下创建 spring 休息服务
creating spring rest services without using spring boot
我已按照 spring.io 上的入门教程构建 REEST 服务 https://spring.io/guides/gs/rest-service/。问题是本教程仅解释如何使用 spring 引导生成带有 tomcat 嵌入的独立 运行 jar。
有没有办法从头开始创建一个项目来生成一个 war 以部署在一个已经存在的 tomcat 实例上?
PS:我在 Whosebug 上找到了一个关于相同问题的先前线程 Spring RESTful Service as a WAR instead of JAR in Tomcat。问题是接受的答案和建议并不能完全解决我的问题,因为我不是在寻找修改独立应用程序 spring 引导项目的方法,以便它可以在外部 tomcat容器,但想找到一个完全不涉及 spring 引导的 'cleaner' 解决方案。 (我不太确定如何在这里表现,在 Whosebug 仍然很新。我希望打开一个新问题是正确的程序)。
您不需要 Spring 引导来创建休息控制器。
请按照 spring 框架文档了解如何设置 MVC
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#spring-web
MVC 设置(DispatcherServlet
)取决于您的 spring 版本,您可以使用 xml 或以编程方式设置:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet
设置完成后,您可以将休息控制器添加到您的应用程序中。请注意,rest controller(@RestController
注释)是一个构造型注释,它结合了 @ResponseBody
和 @Controller
,换句话说,Controller returns 是响应主体中的一个对象,而不是返回视图。
这是一个完美的例子来解释我上面所说的:
http://www.programming-free.com/2014/01/spring-mvc-40-restful-web-services.html
这是另一个例子:
目录布局:
.
├── ./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 http://maven.apache.org/maven-v4_0_0.xsd">
<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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
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);
}
}
构建并运行:
mvn clean install
mvn jetty:start
测试:
> 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)
收到的内容:
{
"msg": "Hello World"
}
我已按照 spring.io 上的入门教程构建 REEST 服务 https://spring.io/guides/gs/rest-service/。问题是本教程仅解释如何使用 spring 引导生成带有 tomcat 嵌入的独立 运行 jar。
有没有办法从头开始创建一个项目来生成一个 war 以部署在一个已经存在的 tomcat 实例上?
PS:我在 Whosebug 上找到了一个关于相同问题的先前线程 Spring RESTful Service as a WAR instead of JAR in Tomcat。问题是接受的答案和建议并不能完全解决我的问题,因为我不是在寻找修改独立应用程序 spring 引导项目的方法,以便它可以在外部 tomcat容器,但想找到一个完全不涉及 spring 引导的 'cleaner' 解决方案。 (我不太确定如何在这里表现,在 Whosebug 仍然很新。我希望打开一个新问题是正确的程序)。
您不需要 Spring 引导来创建休息控制器。
请按照 spring 框架文档了解如何设置 MVC https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#spring-web
MVC 设置(DispatcherServlet
)取决于您的 spring 版本,您可以使用 xml 或以编程方式设置:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet
设置完成后,您可以将休息控制器添加到您的应用程序中。请注意,rest controller(@RestController
注释)是一个构造型注释,它结合了 @ResponseBody
和 @Controller
,换句话说,Controller returns 是响应主体中的一个对象,而不是返回视图。
这是一个完美的例子来解释我上面所说的: http://www.programming-free.com/2014/01/spring-mvc-40-restful-web-services.html
这是另一个例子:
目录布局:
.
├── ./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 http://maven.apache.org/maven-v4_0_0.xsd">
<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
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
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);
}
}
构建并运行:
mvn clean install
mvn jetty:start
测试:
> 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)
收到的内容:
{
"msg": "Hello World"
}