简体   繁体   中英

Why can't Postman find my Spring REST API?

I created a very basic Spring Boot project with REST APIs. I tried connecting it to my Angular app but it was getting some CORS security error so I switched to Postman. I'm trying to test it using Postman but I keep receiving a 404 not found error on Postman. Why am I not able to connect to my backend and post to tester function?

controller

package controllers;

@RestController
public class Handler {
    
    @PostMapping("/tester/{id}")
    public String tester(@PathVariable(value="id")long userid ){
        
        System.out.println("in tester");
        System.out.println(userid);
        
        return "succeeded test";
    }
}

main

package halloween.contest;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
        SecurityAutoConfiguration.class})
public class ContestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ContestApplication.class, args);
    }

}

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration

spring.data.rest.base-path=/

pom.xml

<?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.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>halloween</groupId>
    <artifactId>contest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>contest</name>
    <description>halloween picture contest</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

console

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.6)

2021-10-26 15:44:12.823  INFO 18760 --- [           main] halloween.contest.ContestApplication     : Starting ContestApplication using Java 1.8.0_144 on DESKTOP-VB80TS0 with PID 18760 (C:\Users\Sergio Rodríguez\Documents\Halloween\contest\target\classes started by Sergio Rodríguez in C:\Users\Sergio Rodríguez\Documents\Halloween\contest)
2021-10-26 15:44:12.827  INFO 18760 --- [           main] halloween.contest.ContestApplication     : No active profile set, falling back to default profiles: default
2021-10-26 15:44:16.013  INFO 18760 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-10-26 15:44:16.043  INFO 18760 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-10-26 15:44:16.043  INFO 18760 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-26 15:44:16.310  INFO 18760 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-10-26 15:44:16.310  INFO 18760 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3372 ms
2021-10-26 15:44:19.326  INFO 18760 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-26 15:44:19.342  INFO 18760 --- [           main] halloween.contest.ContestApplication     : Started ContestApplication in 7.201 seconds (JVM running for 7.946)
2021-10-26 15:45:45.138  INFO 18760 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-26 15:45:45.139  INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-10-26 15:45:45.157  INFO 18760 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 17 ms

equivalent cURL command

curl --location --request POST 'localhost:8080/tester/69'

Postman

邮递员请求以 404 响应

To fix the CORS issue, next to @PostMapping("/tester/{id}") add @CrossOrigin(origins = "http://localhost:<port>") where port is the port from which your Angular app is running.

In your target URL, make sure you've included the default Spring Boot port of 8080, eg http://localhost:8080/tester/123 and obviously, please double check that you're doing a POST and not a GET .

Answering my own question now that I figured it out. It wasn't apparent based on the code I provided but I'll leave my post for future help (wording helps).

the problem was visibility of packages. I had my controller class in a different package from the main class.

got a similar problem..then added the below annotations on top of springbootappication where main() exists, then it's working fine for me.

@EnableWebMvc
@ComponentScan("com.*.*")

Also in postman make the below changes...

  1. Disable/uncheck File --> Proxy --> "Use the system proxy"
  2. Disable/uncheck File --> General --> "SSL Certificate Verification"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM