@RestController
public class PdfrmRestController {
// this endpoint only works with the embedded tomcat
@PostMapping(value = "/inputTest")
ResponseEntity<String> inputTest(@RequestBody Inbound in) {
System.out.println("input accepted " + in.getInput().getCorrType());
return ResponseEntity.ok("input valid! " + in.getInput().getCorrType());
}
// this endpoint works either way
@GetMapping("/testCall")
ResponseEntity<String> testCall() throws Exception {
System.out.println("testCall invoked");
return ResponseEntity.ok("test call");
}
}
Inbound.java&pdfrminput.java
public class Inbound {
@JsonProperty("PDF_R_M")
PDFRMInput input;
public PDFRMInput getInput() {
return input;
}
public void setInput(PDFRMInput input) {
this.input = input;
}
}
public class PDFRMInput {
private String corrType;
public String getCorrType() {
return corrType;
}
public void setCorrType(String corrType) {
this.corrType = corrType;
}
}
//embedded tomcat
@SpringBootApplication
public class PdfrmEmbedded {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder()
.bannerMode(Banner.Mode.OFF)
.sources(WebServiceContextSingleton.class)
.run(args);
}
}
//external tomcat (extends is the only change)
@SpringBootApplication
public class PdfrmMain extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder()
.bannerMode(Banner.Mode.OFF)
.sources(WebServiceContextSingleton.class)
.run(args);
}
}
使用SOAPUI:
POST http://localhost:8080/inputTest HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 49
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.5 (Java/16.0.1)
{
"PDF_R_M": {
"corrType": "corrTypeVal"
}
}
运行嵌入式的tomcat版本时的recresults:
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 24
Date: Tue, 28 Jan 2025 21:41:10 GMT
Keep-Alive: timeout=60
Connection: keep-alive
input valid! corrTypeVal
部署到外部tomcat时:
2025-01-28T16:42:59.156-05:00 INFO 27892 --- [ main] org.apache.catalina.startup.Catalina : Server startup in [13536] milliseconds
2025-01-28T16:47:27.172-05:00 DEBUG 27892 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : POST "/pdfrm-webapp/services/rest/inputTest", parameters={}
2025-01-28T16:47:27.258-05:00 DEBUG 27892 --- [nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.fde.pdfrm.rest.PdfrmRestController#inputTest(Inbound)
2025-01-28T16:47:27.330-05:00 DEBUG 27892 --- [nio-8080-exec-5] o.s.web.method.HandlerMethod : Could not resolve parameter [0] in org.springframework.http.ResponseEntity<java.lang.String> com.fde.pdfrm.rest.PdfrmRestController.inputTest(com.fde.pdfrm.data.Inbound): Content-Type 'application/json;charset=UTF-8' is not supported
2025-01-28T16:47:27.339-05:00 WARN 27892 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' is not supported]
2025-01-28T16:47:27.339-05:00 DEBUG 27892 --- [nio-8080-exec-5] o.s.web.servlet.DispatcherServlet : Completed 415 UNSUPPORTED_MEDIA_TYPE
项目树
dmeo
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ ├── PdfrmMain.java
│ ├── Inbound.java
│ ├── PDFRMInput.java
│ └── PdfrmRestController.java
└── resources
└── application.properties
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>3.3.3</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>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipTests>true</skipTests>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pdfrmmain.java
System.out.println(">>> DEBUG-1");
System.out.println(">>>> DEBUG-AAA") ;
用于测试tomcat服务器启动时是否执行方法main
configure
。
Inbound.java
pdfrminput.java
PDFRMRESTCONTROLLER.JAVAapplication.properties
spring.application.name=demo
建造并安装战争向Tomcatmvn clean package
curl -v http://localhost:8080/demo/inputTest \
-H "Content-Type: application/json" \
-d '{"PDF_R_M": {"corrType": "corrTypeVal"}}'
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
* Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> POST /demo/inputTest HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.5.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 40
>
< HTTP/1.1 200
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 24
< Date: Wed, 29 Jan 2025 02:19:10 GMT
<
* Connection #0 to host localhost left intact
input valid! corrTypeVal
>>>> DEBUG-AAA
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.3)
System.out.println(">>> DEBUG-1") ;
System.out.println(">>> DEBUG-2");
。保留弹簧启动横幅消息以确认Spring Boot应用程序是在Tomcat Server中启动的。