【问题标题】:Request method 'GET' not supported in AWS server Spring Boot REST API status 500AWS服务器Spring Boot REST API状态500不支持请求方法“GET”
【发布时间】:2021-06-09 02:50:26
【问题描述】:

我的 Rest API 在本地运行良好,但当我放入相同的 JAR 并从云服务器运行它时,我只能调用 GET API 而不能从 Postmen 调用 POST API。

下面是我的代码。

RestController

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/test")
public class TestController {

@GetMapping("/all")
public String allAccess() {
    return "Public Content.";
}

@PostMapping("/testpostA")
public ResponseEntity<?> testPostA() {
    return ResponseEntity.ok(new MessageResponse("Test POST successfully!"));
}

@PostMapping("/testpostB")
public String testPostB() {
    return "String return POST successfully.";
}
}

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.4.2</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.eurogain</groupId>
<artifactId>WebPortal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WebPortal</name>
<description>Java Spring Boot project for the Eurogain web portal for both internal and external client.</description>
<properties>
    <java.version>15</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</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>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </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>
</dependencies>

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

WebSecurityConfigurationAdapter

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
    // securedEnabled = true,
    // jsr250Enabled = true,
    prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;

@Autowired
private AuthEntryPointJwt unauthorizedHandler;

@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
    return new AuthTokenFilter();
}

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    System.out.println("============================ Inside WebSecurityConfig ===========================Ken");
    http.csrf().disable().authorizeRequests().anyRequest().permitAll();
}}

最后,网络应用程序:

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

在本地运行相同的 JAR 文件,调用所有 POST 和 GET API 没有问题。但是,当我们将同一个 JAR 移动到 AWS EC2 云实例并运行它时,我们只能调用 GET API。在调用任何 POST API 时,Postmen 控制台会抛出以下错误:

但是,调用 GET API 时没有问题:

服务器控制台输出 - 没有错误:

更新 - 第 1 轮

使用 wget 测试,错误 500 也是如此。没有更多信息。

更新 - 第 2 轮 使用 wget POST 进行测试,并在修改 WebSecurityConfig 之后进行测试。这是截图:

服务器控制台在 wget POST 时包含警告:

WebSecurityConfig 的更新代码:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable();
}
}

【问题讨论】:

  • 如果使用 curl 或 wget 会发生什么?
  • @tgdavies 一样。让我使用其他工具更新以上测试结果。
  • wget 默认使用 GET。您需要告诉它使用 POST。
  • @tgdavies,好的,已更新。由于我正在更新代码,删除所有业务逻辑,现在 POST 返回错误 405。请参阅上面的更新内容,了解 wget 的结果以及我更新的 POST 和区域。
  • @tgdavies 请参阅上述最新和更新的详细信息 - “更新 - 第 2 轮”。谢谢。

标签: java spring rest amazon-ec2 postman


【解决方案1】:

试试这样的配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
            }
        };
    }
}

【讨论】:

    【解决方案2】:

    问题是由于 Nginx 反向代理处理造成的。在 localhost 中,我使用的是 HTTP,但在开发服务器中,我们只使用 HTTPS。因此,我对所有 RESTful API URL 使用了 HTTPS。

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 2017-10-21
      • 1970-01-01
      • 2018-12-24
      • 2018-04-01
      • 2023-04-09
      • 1970-01-01
      • 2015-04-23
      • 2015-07-26
      相关资源
      最近更新 更多