Java GET/POST方法未读取邮递员发送的数据,但请求已到达后端,状态代码为200

Java GET/POST方法未读取邮递员发送的数据,但请求已到达后端,状态代码为200,java,spring-boot,rest,Java,Spring Boot,Rest,-----------------------------------------------Pom.xml---------------------------------- @RestController @RequestMapping("/incident") public class EncryptJsonString { @GetMapping("/test") public String getTest(String a)

-----------------------------------------------Pom.xml----------------------------------

@RestController
@RequestMapping("/incident")
public class EncryptJsonString {
  
    @GetMapping("/test")
    public String getTest(String a) { 
        System.out.println("yuty "+a);
        return a; 
        }
}
<?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.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.incident_tool</groupId>
    <artifactId>incdt_tool</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Incdt-Tracker</name>
    <description>webservice for incident management tool</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我已经通过Jax-rsrest对它进行了测试,它工作得很好,问题只出现在springboot中。Postman已成功命中后端代码,但数据未在方法参数中映射。我正在从邮递员处发送文本/普通邮件。

试试这个:

2021-06-09 06:51:48.018  INFO 16256 --- [           main] c.incident.tool.IncdtTrackerApplication  : Starting IncdtTrackerApplication using Java 15.0.2 on LAPTOP-03TT5H9L with PID 16256 (C:\Users\sk464\eclipse-workspace\Incdt-Tracker\target\classes started by sk464 in C:\Users\sk464\eclipse-workspace\Incdt-Tracker)
2021-06-09 06:51:48.021  INFO 16256 --- [           main] c.incident.tool.IncdtTrackerApplication  : No active profile set, falling back to default profiles: default
2021-06-09 06:51:48.680  INFO 16256 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-06-09 06:51:48.733  INFO 16256 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 43 ms. Found 1 JPA repository interfaces.
2021-06-09 06:51:49.285  INFO 16256 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-06-09 06:51:49.296  INFO 16256 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-06-09 06:51:49.296  INFO 16256 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-06-09 06:51:49.442  INFO 16256 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-06-09 06:51:49.442  INFO 16256 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1377 ms
2021-06-09 06:51:49.643  INFO 16256 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-06-09 06:51:49.921  INFO 16256 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-06-09 06:51:50.004  INFO 16256 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-06-09 06:51:50.062  INFO 16256 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.31.Final
2021-06-09 06:51:50.255  INFO 16256 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-06-09 06:51:50.399  INFO 16256 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-06-09 06:51:51.282  INFO 16256 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-06-09 06:51:51.294  INFO 16256 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-06-09 06:51:51.364  WARN 16256 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-06-09 06:51:51.994  INFO 16256 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-06-09 06:51:52.005  INFO 16256 --- [           main] c.incident.tool.IncdtTrackerApplication  : Started IncdtTrackerApplication in 4.38 seconds (JVM running for 5.189)
2021-06-09 06:51:52.006  INFO 16256 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT
2021-06-09 06:51:52.007  INFO 16256 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2021-06-09 06:52:18.351  INFO 16256 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-09 06:52:18.351  INFO 16256 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-06-09 06:52:18.354  INFO 16256 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

Output when Get request is made via Postman:
yuty null

然后使用
http://localhost:8080/incident/test/whatever
。它应该打印
yuty any

这里有两种方法。一个是亚历山德罗刚刚写的。(这是最好的,您的方法是GET,因此必须使用PathVariables或QueryParams)

如果您实际上试图在请求正文中发送纯文本,则应将
@RequestBody
注释添加到方法参数中

像这样:

@RestController
@RequestMapping("/incident")
public class EncryptJsonString {
  
    @GetMapping("/test/{a}")
    public String getTest(@PathVariable("a") String a) { 
        System.out.println("yuty "+a);
        return a; 
        }
}

但请记住,如果您的请求是GET,则应避免发送正文。

您尝试使用的url是什么?你查过日志了吗?在属性中添加debug=true。那就再试一次,汉克斯!我没有注意到“@PathVariable”注释,因为我刚刚启动spring-boot。谢谢!添加
@RequestBody
解决了该问题。我接受这个答案。
@GetMapping("/test")
public String getTest(@RequestBody String a) { 
    System.out.println("yuty "+a);
    return a; 
}