【问题标题】:Request method 'GET' not supported There was an unexpected error (type=Method Not Allowed, status=405)不支持请求方法 \'GET\' 出现意外错误(type=Method Not Allowed,status=405)
【发布时间】:2022-12-10 16:57:28
【问题描述】:

我的场景如下: 有几个实体,我需要为每个实体发送定制设计的电子邮件“按编号”。例如,我正在尝试通过此 URL 为“babs”实体发送​​电子邮件: http://localhost:8081/api/v1/test/babss/sendmail/b1d0c331-35ac-430d-87ca-1718b06351c3

BaBS 的示例实体:

    import com.fasterxml.jackson.annotation.JsonBackReference;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.*;
    import javax.persistence.*;
    import java.util.UUID;

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    @Entity
    @Table(name = "babs")
    public class BaBs {

    @Id
    @Column(name = "id")
    private UUID id;

    @Column(name = "accountCode")
    private String accountCode;

    @Column(name = "accountName")
    private String accountName;

    @Column(name = "eMail")
    private String eMail;
 
    @ManyToOne
    @JsonBackReference
    @JoinColumn(name = "accountid")
    private Account account;

    }

电子邮件发送服务

    import gokhan.mutabakatcore.models.BaBs;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.mail.MailException;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Component;

    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;

    @Component
    public class SentBaBsEmail {

    @Autowired
    private JavaMailSender sender;

    public HttpStatus mailBaBs(BaBs baBs) throws MailException {
        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo(baBs.getEMail());
        mail.setSubject("Trial Mail for Sending BaBs");
        mail.setText("Normally Details from Object ");
        sender.send(mail);
        System.out.println("eMail sent");
        return HttpStatus.GONE;
    }
    
    }

BaBS Controller对应部分

    import gokhan.mutabakatcore.models.BaBs;
    import gokhan.mutabakatcore.services.BaBsService;
    import gokhan.mutabakatcore.utils.SentBaBsEmail;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;

    import javax.servlet.http.HttpServletRequest;
    import javax.validation.Valid;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.UUID;
    import java.util.function.Function;

    @RestController
    @RequestMapping("/api/v1/test/babss")
    public class BaBsController {
    private final BaBsService baBsService;

    public BaBsController(BaBsService baBsService) {
        this.baBsService = baBsService;
    }

    @Autowired
    SentBaBsEmail baBsEmail;

    @RequestMapping(value = "/sendmail/{Id}", method = { RequestMethod.GET, RequestMethod.POST })
    public ResponseEntity<?> eMailBaBsByCustomerId(@PathVariable("Id") UUID uuid){
        try{
            if (!baBsService.findById(uuid).equals(Optional.empty())){

                // Converts Optional Object to Normal Object
                BaBs baBs = (BaBs) toList(baBsService.findById(uuid));
                baBsEmail.mailBaBs(baBs);
                return new ResponseEntity(baBs ,HttpStatus.CREATED);
            }
            return new ResponseEntity<>("Unrecorded BaBS!", HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }
    }

Application.properties 邮件部分

    #SMTP Email Properties
spring.mail.host=mail.xyz.com
spring.mail.port=587
spring.mail.username=info@xyz.com
spring.mail.password=password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.starttls.enable=true
spring.mail.properties.mail.starttls.required=true

相关的 POM.xml 依赖项

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

我得到的错误:

    This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Aug 06 08:39:31 EET 2022
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

如果你能告诉我这个问题,我将不胜感激......

【问题讨论】:

  • 看起来你的请求映射中有两个请求方法,“RequestMethod.GET,RequestMethod.POST”!每种方法都必须有一个请求方法。
  • 我在 stackoverflow 上找到了这个解决方案,但在我的案例中不起作用。我保留了它,以防有人提供相同的服务。

标签: spring-boot methods get request


【解决方案1】:

添加这个

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

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 2020-08-22
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2017-06-14
    相关资源
    最近更新 更多