gpt4 book ai didi

java - 使用消息 "message": "Content type ' text/plain;charset=UTF- 8' not supported" 测试 Post API 时获取 415 状态代码

转载 作者:太空宇宙 更新时间:2023-11-04 09:57:29 26 4
gpt4 key购买 nike

使用Postman测试REST Controller (使用SpringBoot开发)时,反复获取415状态码。

仅在特定帖子请求时发生。其他帖子请求工作正常。模仿了工作岗位 Controller ,但没有成功。

@RequestMapping(value="/addtheatre", method= RequestMethod.POST)
public HttpStatus addTheatre(@RequestBody Theatre theatre ) {
theatrerepository.save(theatre);
return HttpStatus.OK;

}

剧院实体如下:-包 com.example.Model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.sql.Time;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


@Entity(name="Theatre")
public class Theatre {

Theatre()
{}
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="Id")
private long id;
@Column(name="name")
@NotNull
private String name;
@Column(name="address")
@NotNull
private String address;
@Column(name="city")
@NotNull
private String city;
@Column(name="is_active")
@NotNull
private Boolean is_active;
@Transient
private List<TheatreHall> halls;
@Transient
private Map <Movie,LinkedList<Time>> map;

public Boolean getIs_active() {
return is_active;
}

public void setIs_active(Boolean is_active) {
this.is_active = is_active;
}

public List<TheatreHall> getHalls() {
return halls;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void setHalls(List<TheatreHall> halls) {
this.halls = halls;
}

public long getId() {
return id;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public Map<Movie, LinkedList<Time>> getMap() {
return map;
}

public void setMap(Map<Movie, LinkedList<Time>> map) {
this.map = map;
}

@Override
public String toString() {
return "Theatre{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", city='" + city + '\'' +
", is_active=" + is_active +
", halls=" + halls +
", map=" + map +
'}';
}

}

Postman测试数据如下:-

端点:-localhost:8080/addtheatre

请求:-

{   
"name":"PVR Mall",
"address":"Andheri",
"city":"Mumbai",
"is_active":"true"
}

回应:-

{
"timestamp": "2018-12-26T08:21:43.269+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/json;charset=UTF-8' not supported",
"path": "/addtheatre"
}

预计此 POST Controller 状态正常。

最佳答案

将 is_active 更改为 isActive,将 setter 更改为 setIsActive。我怀疑 Jackson 由于命名约定而无法找到 is_active 的相关 setter,因此无法解析该对象

关于java - 使用消息 "message": "Content type ' text/plain;charset=UTF- 8' not supported" 测试 Post API 时获取 415 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com