无法在Spring Boot中实现POST方法

时间:2019-05-28 12:02:00

标签: spring-boot post postman

我正在尝试实现一个简单的POST方法,但是无法实现。

尝试了“ stackoverflow”中提到的几种解决方案,但仍然会出错。

我的控制器类

@RestController
@RequestMapping("api/public")
public class PublicRestApiController {

@GetMapping("users")
public List<User> users(){
    return this.userRepository.findAll();
}

@PostMapping(path="/insert",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.ACCEPTED)
 public User add(@RequestBody UserRequest account) {
    User response = new User(account.getUsername(),account.getPassword(),account.getRoles(), account.getPermissions());
    return response;
    }

}

UserRequest类

public class UserRequest {

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public int getActive() {
    return active;
}
public void setActive(int active) {
    this.active = active;
}
public String getRoles() {
    return roles;
}
public void setRoles(String roles) {
    this.roles = roles;
}
public String getPermissions() {
    return permissions;
}
public void setPermissions(String permissions) {
    this.permissions = permissions;
}
private String username;
private String password;
private int active;
private String roles = "";
private String permissions = "";

}

用户类别

@Entity
@Table(name = "USER_DETAILS")
public class User {

@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@Column(nullable = false)
private String username;

@Column(nullable = false)
private String PASSWORD;

@Column
private int ACTIVE;

@Column
private String ROLES = "";

@Column
private String PERMISSIONS = "";

public User(String username, String password, String roles, String permissions){
    this.username = username;
    this.PASSWORD = password;
    this.ROLES = roles;
    this.PERMISSIONS = permissions;
    this.ACTIVE = 1;
}

public User(){}

public int getId() {
    return id;
}

public String getUsername() {
    return username;
}

public String getPassword() {
    return PASSWORD;
}

public int getActive() {
    return ACTIVE;
}

public String getRoles() {
    return ROLES;
}

public String getPermissions() {
    return PERMISSIONS;
}

public void setId(int id) {
    this.id = id;
}

public void setUsername(String username) {
    this.username = username;
}

public void setPassword(String password) {
    this.PASSWORD = password;
}

public void setActive(int active) {
    this.ACTIVE = active;
}

public void setRoles(String roles) {
    this.ROLES = roles;
}

public void setPermissions(String permissions) {
    this.PERMISSIONS = permissions;
}
}

出现此错误消息。

2019-05-28 17:08:41.808  WARN 7712 --- [nio-8443-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]

GET方法在同一控制器中运行良好,只是在尝试实现POST工具时出错。

Request screenshot through Postman

安全配置

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
      .antMatchers("/api/public/users").hasRole("ADMIN")
      .antMatchers("/api/public/insert").hasRole("ADMIN")
      .and()
      .httpBasic();
}

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

}

应用程序类

@SpringBootApplication(scanBasePackages = {"rc.bootsecurity"})
public class BootSecurityApplication {

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

@Bean
public ServletWebServerFactory servletContainer() {
    // Enable SSL Trafic
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };

    // Add HTTP to HTTPS redirect
    tomcat.addAdditionalTomcatConnectors(httpToHttpsRedirectConnector());

    return tomcat;
}

private Connector httpToHttpsRedirectConnector() {
    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    connector.setScheme("http");
    connector.setPort(8082);
    connector.setSecure(false);
    connector.setRedirectPort(8443);
    return connector;
    }
}

1 个答案:

答案 0 :(得分:0)

  

可能的解决方案1:

  1. 打开邮递员
  2. 转到“使用文件部分设置”
  3. 关闭自动跟随重定向

enter image description here


  

可能的解决方案2:

添加一个extra / (trailing slash) to your endPointapi/public/insert更改为api/public/insert/


邮递员支持部门关于我发送了POST请求,但邮递员似乎正在发送GET请求。

  

这主要是由于服务器端重定向。如果邮递员收到   301或302响应代码,它将自动遵循重定向。   与浏览器中的行为类似,原始请求方法将   不保留,将使用GET发出重定向的请求   方法。如果要禁用自动跟随重定向行为,或   如果您对此还有其他疑问,请查看相关内容   重定向文档

Postman Support Interceptor extension Git Issue


更新:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf().disable()
      .authorizeRequests()
      .antMatchers("/api/public/users").hasRole("ADMIN")
      .antMatchers(HttpMethod.POST,"/api/public/insert").hasRole("ADMIN")
      .anyRequest().authenticated();
}