繁体   English   中英

向 spring 发送请求时找不到 404 从 Angular 中运行的 api

[英]404 not found when sending requests to spring boot api from Angular running in Apache2

我有一个带有 spring-boot 后端 api 和 Angular10 前端的应用程序。 在本地运行这些我没有问题,一切运行正常。 我已经在 Debian VPS 上部署了我的应用程序,其中 Angular 构建在 apache2 上运行。 但是当试图在那里发送请求时,我得到一个 402 not found tot 请求的 URL。

在 Apache 中,我配置了一个反向代理来访问 spring 引导:

<VirtualHost *:80>
  DocumentRoot /var/www/html
  ProxyPass /api/ http://localhost:8080/
  ProxyPassReverse /api/ http://37.128.150.186/api/
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

And in my environment.ts file in Angular I specified the base of the url like "/api/" so every request will go to through these URLS with the base port of Apache (:80). 但是当我这样做时,我得到一个 404 未找到。 但是当我 go 到 URL 中的 /api/ 时,我确实看到了 Spring 引导的“家庭控制器”。

尝试向 spring-boot controller 发送帖子时在浏览器中请求:

Request URL: http://37.128.150.186/api/users
Request Method: POST
Status Code: 404 
Remote Address: 37.128.150.186:80
Referrer Policy: strict-origin-when-cross-origin

错误响应:

"timestamp": "2021-05-02T21:15:46.970+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "",
    "path": "//users"

controller 在此示例中我尝试将请求发送到:

@RestController
@RequestMapping(value = "/api/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    private final UserService userService;
    private final PasswordEncoder encoder;

    public UserController(final UserService userService, final PasswordEncoder encoder) {
        this.userService = userService;
        this.encoder = encoder;
    }

    @GetMapping
    public ResponseEntity<List<UserDTO>> getAllUsers() {
        return ResponseEntity.ok(userService.findAll());
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserDTO> getUser(@PathVariable final Long id) {
        return ResponseEntity.ok(userService.get(id));
    }

    /**
     *
     * @param userDTO user type to create new user
     * @return user written to the database
     */
    @PostMapping
    public ResponseEntity<Long> createUser(@RequestBody @Valid final UserDTO userDTO) {
        // create new user
        UserDTO newUser = new UserDTO();
        // set email
        newUser.setEmail(userDTO.getEmail());
        // set firstname
        newUser.setFirstName(userDTO.getFirstName());
        // set lastname
        newUser.setLastName(userDTO.getLastName());
        // set hashed password
        newUser.setPassword(encoder.encode(userDTO.getPassword()));
        // return new user with hashed password
        return new ResponseEntity<>(userService.create(newUser), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Void> updateUser(@PathVariable final Long id,
            @RequestBody @Valid final UserDTO userDTO) {
        userService.update(id, userDTO);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable final Long id) {
        userService.delete(id);
        return ResponseEntity.noContent().build();
    }

}

再次,当我在本地计算机上运行该应用程序时,一切正常。 我忘了配置什么吗?

该错误来自 spring 本身。 我相信错误在于您在一个配置文件中配置了上下文路径,但在另一个配置文件中没有配置。 (如果你点击http://37.128.150.186/api/api/users你会得到回应)。

也可能是一个问题是ProxyPassReverse /api/ http://37.128.150.186/api/我不确定 /api/ 是否需要附加到 IP。

没有@Autowired@Inject注解就不能添加构造函数参数
会像那个视图参考链接

@Autowired
public UserController(@Qualifier("questionService") UserService userService, @Qualifier("encoder") PasswordEncoder encoder) {
     this.userService = userService;
     this.encoder = encoder;
}

或者

@Inject
public UserController(@Named("questionService") UserService userService, @Named("encoder") PasswordEncoder encoder) {
     this.userService = userService;
     this.encoder = encoder;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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