【问题标题】:Spring Boot - PostMapping does not working [duplicate]Spring Boot - PostMapping 不起作用[重复]
【发布时间】:2021-01-20 20:12:53
【问题描述】:

我正在开发 Spring Boot api 项目。 @PostMapping 注释未运行。为什么?我添加了断点但没有停止??请帮忙。

pom.xml

...
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

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

ApplicationUser.java

public class ApplicationUser {
    private long id;
    private String name;

    // constructors, getters-setters
}

ApplicationUserController.java

@RestController
@RequestMapping("/api/v1/users")
public class ApplicationUserController {
    public static final List<ApplicationUser> USERS = Arrays.asList(
        new ApplicationUser(1, "Jeff"),
        new ApplicationUser(2, "Mark"),
        new ApplicationUser(3, "Bill")
    );

    @GetMapping(value="/{userId}", produces="application/json")
    public ResponseEntity<ApplicationUser> getUser(@PathVariable("userId") Long userId) {
        return ResponseEntity.ok(
            USERS.stream()
            .filter(user -> userId.equals(user.getId()))
            .findFirst()
            .orElseThrow(() -> new IllegalStateException(
                "User " + userId + "does not exists"
            ))
        );
    }

    @PostMapping(value="/add", consumes="application/json")
    public void addUser(@RequestBody ApplicationUser user) {
        for (long i = 0L; i < USERS.size(); i++)
            if (user.getId() == USERS.get((int)i).getId())
                throw new IllegalStateException("User " + user.getId() + " already exists");
        
        USERS.add(user);
    }
}

ApplicationSecurityConfig.java

@Configuration
@EnableWebSecurity
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic();
    }
}

我尝试了以下命令

~$ curl http://localhost:8080/api/v1/users/add -X POST -H 'Content-type: application/json' -d '{"id": 4, "name": "Steve"}' -u user:-spring-security-password-
(no response)

在上面的命令运行后,我尝试了以下操作

~$ curl http://localhost:8080/api/v1/users/1 -u user:-spring-security-password-
{"id":1,"name":"Jeff"}

~$ curl http://localhost:8080/api/v1/users/4 -u user:-spring-security-password-
{"timestamp":"2021-01-20T19:53:19.205+00:00","status":500,"error":"Internal Server Error","message":"","path":"/api/v1/users/4"}

-- 编辑--

我尝试向 Postman 提出 POST 请求,得到 401 Unauthorized。当尝试GET请求时,运行正常。

【问题讨论】:

  • List.size() 返回一个int
  • 只是一个想法,但这可能是您如何使用 curl 的问题吗?尝试另一种方法,例如邮递员,看看您是否仍然可以重现此问题。
  • @PeterMmm 我将所有 long 和 Long 值更改为 int 和 Integer 但没有更改任何内容:(
  • @Jason 我尝试向邮递员发出 POST 请求,邮递员说401 Unauthorized。当我用邮递员尝试 GET 请求时,运行正常。
  • 尝试在您的安全配置中禁用表单登录。

标签: java spring-boot


【解决方案1】:

Arrays.asList() 返回的列表不支持add()。如果仍然尝试添加,则会抛出UnsupportedOperationException。尝试初始化列表,如下所示。

public static final List<ApplicationUser> USERS = new ArrayList<>(Arrays.asList(
        new ApplicationUser(1, "Jeff"),
        new ApplicationUser(2, "Mark"),
        new ApplicationUser(3, "Bill")
    ));

【讨论】:

  • 在你的 restController 上使用 @Slf4j 并在 USERS.add(user); 之后的 addUser() 方法中写入 log.info(USERS.toString())。然后发出 POST 请求。希望我们可以通过查看日志来了解发生了什么。
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 2021-07-15
相关资源
最近更新 更多