Java &引用;内容类型';应用程序/json;字符集=UTF-8';“不支持”;在弹簧休息应用中
当我在localhost:8080/api/users上执行POST请求以创建新用户时,我得到以下错误:Java &引用;内容类型';应用程序/json;字符集=UTF-8';“不支持”;在弹簧休息应用中,java,spring,rest,httprequest,postman,Java,Spring,Rest,Httprequest,Postman,当我在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"
{
"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(应用程序/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);
}
public User createUser(@Valid@RequestBody User){
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword());
user.setRoles(新的HashSet(roleRepository.findAll());
返回userRepository.save(用户);
}
编辑 这是我的用户类:
@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;
}
}
@实体
@表(name=“user”)
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(值={“createdAt”,“updatedAt”},
allowGetters=true)
公共类用户实现可序列化{
私有静态最终长serialVersionUID=1L;
公共用户(){
超级();
//TODO自动生成的构造函数存根
}
@身份证
@列(name=“user\u id”)
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@列(name=“user\u name”)
私有字符串名称;
@列(name=“user\u email”)
私人字符串电子邮件;
@列(name=“user\u password”)
@不空白
私有字符串密码;
@列(name=“user\u status”)
私有字符串状态;
@列(name=“user\u tel”)
私人电话;
@列(name=“用户确认”)
私有字符串确认;
@列(name=“user\u birth\u date”)
@时态(TemporalType.DATE)
私人日期出生日期;
@列(nullable=false,Updateable=false)
@时态(TemporalType.TIMESTAMP)
@创建数据
私人日期创建日期;
@列(nullable=false)
@时态(TemporalType.TIMESTAMP)
@最后修改日期
私人日期更新日期;
@JsonManagedReference
@许多
@JoinTable(name=“user\u role”,joinColumns=@JoinColumn(name=“user\u id”),inverseJoinColumns=@JoinColumn(name=“role\u id”))
私人设定角色;
@列(name=“username”)
@不空白
私有字符串用户名;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getUsername(){
返回用户名;
}
public void setUsername(字符串用户名){
this.username=用户名;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共字符串getTel(){
返回电话;
}
公共电话(字符串电话){
this.tel=tel;
}
公共字符串getConfirmation(){
返回确认;
}
公共无效集合确认(字符串确认){
这个。确认=确认;
}
公开日期出生日期{
返回出生日期;
}
公共无效设置出生日期(出生日期){
this.birth\u date=出生日期;
}
公共日期getCreatedAt(){
返回createdAt;
}
公共日期getUpdatedAt(){
返回更新数据;
}
公共无效设置日期日期(日期更新日期){
this.updatedAt=updatedAt;
}
公共集getRoles(){
返回角色;
}
公共无效集合角色(集合角色){
this.roles=角色;
}
}
将@products(MediaType.APPLICATION\u JSON)
设置到您的函数中,将其设置为JSON mime类型
您可以查看更多信息。您可以尝试以下方法
@PostMapping(value=“/rest/account/json”,consumes={“application/json”})
我可以通过删除@JsonManagedReference来解决这个问题。您指定的内容类型=application/json。我认为您可能还需要检查“Accept”头属性(application/json)
它清楚地说明了不支持的媒体类型,这意味着由于任何问题,无法完成对操作的调用。因此,请检查您的服务操作要求什么,以及您是否正确发送了所有字段。 大多数情况下都存在映射问题。检查控制台是否有错误。您可以改用“application/json”
在我的案例中,有一个Jackson失败,记录为警告:
无法评估类型[[简单类型,类***]]的Jackson反序列化:com.fasterxml.Jackson.databind.JsonMappingException:属性[…]的setter定义冲突
我意外地使一个二传程序过载,因此Jackson无法解决此问题,spring抛出了415。可能有人错误地为同一属性保留了两个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()"
在这种情况下,只需删除该字段的额外getter。我在Jackson反序列化时也遇到了这个错误。我的情况与Klaudia的响应更为密切相关,我在POJO中有一个成员字段,用于一个用@JsonIgnore标记的服务,但为了简洁起见,我将getter和setter重命名为getService()和setService(),而不是匹配较长命名服务接口的变量名。将@JsonIgnore添加到不同名称的getter和setter中解决了此错误。这将是@Consumes(MediaType.APPLICATION\u JSON)“类型不匹配:无法从MediaType转换为字符串[]”@Ouissal:再试一次,MediaType.APPLICATION\u JSON\u VALUE instea