在测试带有消息“ message”的Post API时获得415状态代码:“内容类型'text / plain; charset = UTF-8'不支持”

时间:2018-12-26 08:34:18

标签: java spring spring-boot

使用Postman测试REST控制器(由SpringBoot开发)时,反复获得415状态代码。

仅在发生特定的帖子请求时发生。其他职位要求正常。已经将工作中的后控制器模仿为失败的控制器,但是没有运气。

@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 +
                '}';
    }

}

邮递员测试数据如下:-

端点:-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控制器的OK状态。

4 个答案:

答案 0 :(得分:0)

如果您使用@RestController来适应@RequestMapping的默认值,请确保同时将application/json设置为Content-TypeAccept标头。

如果您需要精确发送UTF-8编码的json,则需要显式设置consumes属性:

@RequestMapping(value = "...", method = ...,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)

另外,布尔型setter可能存在一个隐式的Jackson问题。尝试将is_active字段(不符合约定)更改为active,它的getter \ setter为:

private boolean active;

@JsonProperty(value="is_active")       //if needed  
public boolean isActive() {
    return this.active;
}

public void setActive(boolean active) {
    this.active = active;
}

答案 1 :(得分:0)

这可能是因为缺少消耗,并在Web Service方法的@RequestMapping内部生成值
可以添加那些:

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

}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可能会检查headerRequest !!!!!,我认为头部的设置有些错误。关注“ Content-Type”,应将其设置为“”