【问题标题】:"Could not find acceptable representation" 406“找不到可接受的表示”406
【发布时间】:2019-06-27 18:25:54
【问题描述】:

我正在尝试做一个POST 操作:

我的控制器是:

@RestController
@RequestMapping({"/contacts"})
public class ContactController {
...
 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, 
            produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
  public Contact create(@RequestBody Contact contact){
      return repository.save(contact);
  }

Contact 模型是这样的:

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class Contact {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;
    private String phone;

    public Contact(Long id, String name, String email, String phone) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.phone = phone;
    }
    public Contact(String name, String email, String phone) {
        super();
        this.name = name;
        this.email = email;
        this.phone = phone;
    }
    public Contact() {
        super();
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

我使用 POSTMAN 来做 POST 操作,所以:

url:  http://localhost:8080/contacts (POST)body: JSON(application/json)--> {
"name": "Desperados",
"email": "fra@jot.it",
"phone": "88870999"
}
header: Content-type: application-json

我得到了这个例外:

{
    "timestamp": "2019-06-27T10:07:07.803+0000",
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/contacts"
}

谁能帮帮我?

【问题讨论】:

    标签: java spring rest spring-boot postman


    【解决方案1】:

    1) 默认情况下,Rest 控制器使用并生成 JSON,您可能不需要提及它。 2) 确保在 PostMan 中指定内容类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多