![](/img/trans.png)
[英]Content type 'application/json;charset=UTF-8' not supported
[英]"Content type 'application/json;charset=UTF-8' not supported" in Spring Rest application
当我在localhost:8080/api/users上执行 POST 请求以创建新用户时,出现以下错误:
{
"timestamp": "2018-05-28T09:44:55.704+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/json;charset=UTF-8' not supported",
"path": "/api/users/"
}
是请求的主体,选择了 JSON (application/json)。 即使我删除角色并将其保持为空,它也会出现相同的错误。
标头的内容类型也是 application/json。
这是我的控制器:
@PostMapping("/api/users" )
public User createUser(@Valid @RequestBody User user) {
securityService.autologin(user.getUsername(), user.getPassword());
return userService.createUser(user);
}
UserService 中的 createUser 函数:
public User createUser(@Valid @RequestBody User user) {
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
user.setRoles(new HashSet<>(roleRepository.findAll()));
return userRepository.save(user);
}
这是我的用户类:
@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true)
public class User implements Serializable{
private static final long serialVersionUID = 1L;
public User() {
super();
// TODO Auto-generated constructor stub
}
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "user_name")
private String name;
@Column(name = "user_email")
private String email;
@Column(name = "user_password")
@NotBlank
private String password;
@Column(name = "user_status")
private String status;
@Column(name = "user_tel")
private String tel;
@Column(name = "user_confirmation")
private String confirmation;
@Column(name = "user_birth_date")
@Temporal(TemporalType.DATE)
private Date birth_date;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createdAt;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updatedAt;
@JsonManagedReference
@ManyToMany
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
@Column(name = "username")
@NotBlank
private String username;
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 getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getConfirmation() {
return confirmation;
}
public void setConfirmation(String confirmation) {
this.confirmation = confirmation;
}
public Date getBirth_date() {
return birth_date;
}
public void setBirth_date(Date birth_date) {
this.birth_date = birth_date;
}
public Date getCreatedAt() {
return createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
我能够通过删除@JsonManagedReference 来解决它。
在我的情况下,Jackson 失败,记录为警告:
Failed to evaluate Jackson deserialization for type [[simple type, class ***]]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property [...]
我不小心超载了一个二传手,所以杰克逊无法解决它,春天抛出了 415。
你可以尝试类似的东西
@PostMapping(value="/rest/account/json", consumes={"application/json"})
可能会出现有人错误地为同一属性保留 2 个 getter 并且 Jackson 反序列化无法评估的情况。
它抛出
Failed to evaluate Jackson deserialization for type [[simple type,
class com.org..*..*]]:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Conflicting getter definitions for property \"field_name\":
com.org..*..*#getterBName() vs com.org..*..*#gettername()"
在这种情况下,只需删除该字段的额外吸气剂。
我也遇到了这个错误作为杰克逊反序列化的警告。 我的情况与 Klaudia 的响应更密切相关,我的 POJO 上有一个成员字段,用于标记为 @JsonIgnore 的服务,但为了简洁起见,我已将我的 getter 和 setter 重命名为 getService() 和 setService()与更长命名的服务接口匹配的变量名称。 将 @JsonIgnore 添加到不同命名的 getter 和 setter 解决了错误。
它清楚地表明不受支持的媒体类型,这意味着由于任何问题,无法完成对操作的调用。 因此,请检查您的服务操作要求什么,以及您是否正确发送了所有字段。 大多数时候存在映射问题。 检查控制台是否有错误。
在PATCH
请求中使用以下内容时,我也遇到此错误:
public Order markOrderAsPayed(@RequestBody final MultiValueMap<String, String> values) {
...
}
MultiValueMap 来自org.springframework.util
。 将 Map 更改为Map<String, String>
为我解决了这个问题。
抱歉,但我无法解释原因。
这篇文章可能为时已晚。
如果你可以在你的控制器类中添加这个媒体类型为 consumes={MediaType.APPLICATION_JSON_UTF8_VALUE} 你可以通过这个。
尝试从标头 Content-type 中删除字符集,仅应用程序 json,它可能会起作用。
将@Produces(MediaType.APPLICATION_JSON)
设置为您的函数以将其设置为 json mime 类型。
您可以查看该 restapi以获取更多信息。
我在使用@PutMapping 和@RequestBody MultiValueMap<String,String> 时遇到了同样的问题。 我将 MultiValueMap 更改为 Map 并且它起作用了,就像上面 edta 所建议的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.