我试图使用Spring和Jackson来处理休息的端点和对象映射。 当使用带有Spring-Boot-Starter-Web随附的嵌入式tomcat时,下面显示的两个端点如预期所示。 当我通过在主要类上扩展“ SpringBootservleTinializer”来切换到外部tomcat实例时,服务启动启动和TestCall Endpoint有效,但是InputTest会失败,而错误:'content-type'应用程序/json; charset; charset = utf-88 “不支持”。 我尝试在端点上手动应用各种消费/生产设置,而默认值没有运气。 我能够找到有关JSON对象映射问题的指示,但是我对映射为何与嵌入式tomcat一起使用,但在同一版本的外部TOMCAT上产生截然不同的结果。 谁能指向正确的方向? 预先感谢!

问题描述 投票:0回答:1

@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; } }

每个版本的main方法:

//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
java json spring spring-boot tomcat
1个回答
0
投票
请注意,我在此处使用不同的符号,并使用

System.out.println(">>> DEBUG-1");

System.out.println(">>>> DEBUG-AAA") ;
用于测试tomcat服务器启动时是否执行方法

main

和方法
configure

Inbound.java

pdfrminput.java

PDFRMRESTCONTROLLER.JAVA

application.properties
spring.application.name=demo

建造并安装战争向Tomcat

build
  • mvn clean package
  • 安装
  • 副本目标/演示。
  • tomcat/bin/startup.sh

-Test
使用卷曲命令

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

检查tomcatlogs
  • tomcat/logs/catalina.out
  • >>>> DEBUG-AAA . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.3.3)
搜索日志内容,我们可以看到执行了SpringApplicationBuilder Configure(SpringApplicationBuilder应用程序),但是未执行main(String [] ARGS)。 (因为日志看不到

System.out.println(">>> DEBUG-1") ;

,并且执行结果

System.out.println(">>> DEBUG-2");

.
保留弹簧启动横幅消息以确认Spring Boot应用程序是在Tomcat Server中启动的。

© www.soinside.com 2019 - 2024. All rights reserved.