[英]Why does Postman error "unsupported media type" for a POST request in spring boot
我有一个实体 class State
扩展了抽象 class Location
并实现了它的抽象方法。 当我在 postman 上发出PostMapping
请求并将内容类型设置为 Json 时,我收到一个错误不支持的媒体类型,如下所示:
"error:" Unsupported Media Type,
"trace": org.springframework.web.HttpMediaTpeNotSupportedException:Content type:'application/json;...
摘要 class:
@MappedSuperclass
public abstract class Location {
@Id
@Column(name="id")
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@Column(name = "long")
private String longitude;
@Column(name = " lat")
private String latitude;
public Location (){}
//assessors & mutators ommited with abstract methods
}
具体State实体class:
@Table(name="State")
public class State extends Location {
@Pattern(regexp = "[0-9]{5}")
private String sateCode;
@JsonManagedReference
@OneToOne(mappedBy="state",
cascade=CascadeType.All, fetch=FetchType.Lazy)
private City city = new City();
//constructors & other implementations omitted
@Override
public void setCity(String city){
this.city=city
}
public void setStateCode(String code){
this.stateCode = stateCode;
}
///getter omitted
}
具体城市实体class:
@Table(name = "City");
public class City extends Location {
@JsonBackReference
@OneToOne(cascade={
CascadeType.DeTACH, CascadeType. MERGE})
@JoinColumn(name = "state_city_id")
private State state;
//constructors & other implementations omitted
@Override
public void setState(State state) {
this.state=state;
}
}
}
Controller:
@Controller
@RequestMapping(path = "location/destination")
public class LocationController{
@Autowired
@Qualifier("stateImpl")
private LocationService service;
@PostMapping(value = "/state", consumes = org.springframework.http.MediaType.APPLICATION_JSON_VALUE, produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
public void save( @Valid @RequestBody State s ,BindingResult theBindingResult,
HttpServletResponse r) throws IOException{
if(theBindingResult.hasErrors()) {
throw new InputException("Wrong data input!");
}
service.save(s);
r.sendRedirect("");
}
@GetMapping()
public ResponseEntity<Object>findAll(){
return new ResponseEntity<Object>(service.findAll(),HttpStatus.OK);
}
}
postman 上的错误消息:
"status": 415,
"error": "Unsupported Media Type",
"trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8 not supported\r\n\tat...'
当我更改State
实体 class 中的city
字段时,代码有效:
@Entity
@Table(name="State")
public State extends Location {
@JsonBackReference
@OneToOne(mappedBy="state",
cascade=CascadeType.All, fetch=FetchType.Lazy)
private City city = new City();
}
City
实体中的state
字段如下所示:
@Entity
@Table(name="City")
public class City extends Location {
@JsonManagedReference
@OneToOne(cascade={
CascadeType.DeTACH, CascadeType. MERGE})
@JoinColumn(name = "state_city_id")
private State state;
}
```.
Though I'm yet to understand what really happened behind the scenes.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.