内容类型' application / json; charset = UTF-8'不支持

时间:2018-02-14 08:06:26

标签: java json hibernate spring-mvc

我正在使用java spring框架创建新闻稿API。每当我将API作为一个带有请求模型的帖子来获取此权限时,org.springframework.web.HttpMediaTypeNotSupportedException。

这是我的简报型号

@Entity
@Table(name = "ns_newsletters")
public class Newsletter extends DomainObject {

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "newsletter_gen")
@TableGenerator(name = "newsletter_gen", table = "ns_newsletter_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "NewsletterId_Gen", initialValue = 1, allocationSize = 1)
@Column(name = "subscriber_id")
private int subscriberId;

@Column(name = "subscriber_email_address")
private String subscriberEmailAddress;

@Column(name = "is_subscribe")
public boolean isSubscribe;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "gender")
private String gender;

@JsonManagedReference
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "ns_newsletter_mailer_list_linkage", joinColumns = {@JoinColumn(name = "subscriber_id")},
        inverseJoinColumns = {@JoinColumn(name = "newsletter_mailer_id")})
private Set<NewsletterMailerList> mailerLists;


public Newsletter() {
    super();
}

public Newsletter(String createdBy, Timestamp creationDate, int version, Timestamp lastModifiedDate,
                  String lastModifiedBy, RecordStatus recordStatus) {
    super(createdBy, creationDate, version, lastModifiedDate, lastModifiedBy, recordStatus);
}

 public Newsletter(Set<NewsletterMailerList> mailerLists, int subscriberId, String subscriberEmailId, boolean isSubscribe, String firstName, String lastName, String sex) {
    super();
    this.subscriberId = subscriberId;
    this.subscriberEmailAddress = subscriberEmailId;
    this.isSubscribe = isSubscribe;
    this.firstName = firstName;
    this.lastName = lastName;
    this.gender = sex;
    this.mailerLists = mailerLists;
}

public int getSubscriberId() {
    return subscriberId;
}

public void setSubscriberId(int subscriberId) {
    this.subscriberId = subscriberId;
}

public String getSubscriberEmailAddress() {
    return subscriberEmailAddress;
}

public void setSubscriberEmailAddress(String subscriberEmailAddress) {
    this.subscriberEmailAddress = subscriberEmailAddress;
}

public boolean isSubscribe() {
    return isSubscribe;
}

public void setSubscribe(boolean subscribe) {
    isSubscribe = subscribe;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

public Set<NewsletterMailerList> getMailerLists() {
    return mailerLists;
}

public void setMailerLists(Set<NewsletterMailerList> mailerLists) {
    this.mailerLists = mailerLists;
}
}

这是我的NewsletterMailerList模型

@Entity
@Table(name = "ns_newsletter_mailer_list")
public class NewsletterMailerList extends DomainObject {

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "newsletter_mailer_list_gen")
@TableGenerator(name = "newsletter_mailer_list_gen", table = "ns_newsletter_mailer_list_id_gen", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "NewsletterMailerList_Gen", initialValue = 1000, allocationSize = 1)
@Column(name = "newsletter_mailer_id")
private int newsletterMailerId;

@Column(name = "mailer_list_name")
private String mailerListName;

@Column(name = "description")
private String description;

@JsonBackReference
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "mailerLists")
private Set<Newsletter> newsletters;

public Set<Newsletter> getNewsletter() {
    return newsletters;
}

public void setNewsletter(Set<Newsletter> newsletters) {
    this.newsletters = newsletters;
}

public int getNewsletterMailerId() {
    return newsletterMailerId;
}

public void setNewsletterMailerId(int newsletterMailerId) {
    this.newsletterMailerId = newsletterMailerId;
}

public String getMailerListName() {
    return mailerListName;
}

public void setMailerListName(String mailerListName) {
    this.mailerListName = mailerListName;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Set<Newsletter> getNewsletters() {
    return newsletters;
}

public void setNewsletters(Set<Newsletter> newsletters) {
    this.newsletters = newsletters;
}
}

我将包含类型作为application / json。 我是新来做这种事情。请帮助我,为什么我收到此错误。如果您需要更多信息,请告诉我们。

这是时事通讯控制器

package com.neostencil.modules.newslettermanagement.controller;


import com.neostencil.framework.base.BaseResponse;
import com.neostencil.framework.enums.StatusType;
import com.neostencil.framework.utilities.common.CollectionUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping(value = "api/v1")
@Api(value = "Newsletter", description = "Rest API for Newsletter 
operations", tags = "Newsletter API")
public class NewsletterController {

@Autowired
NewsletterService newsletterService;

@RequestMapping(value = "/newsletters", method = RequestMethod.POST)
public ResponseEntity<BaseResponse> addNewsletter(RequestEntity<SingleNewsletterRequest> request) {

    ResponseEntity<BaseResponse> response = null;
    BaseResponse baseResponse = new BaseResponse();
    List<String> messages = new ArrayList<>();

    if (request.getBody() == null) {
        baseResponse.setStatus(StatusType.NOT_FOUND);
        messages.add("Newsletter request is empty or null");
        baseResponse.setMessages(messages);
        response = new ResponseEntity<BaseResponse>(baseResponse, HttpStatus.BAD_REQUEST);
    } else {
        Newsletter newsletter = request.getBody().getNewsletter();
        Set<NewsletterMailerList> mailerLists = request.getBody().getNewsletter().getMailerLists();

        if (CollectionUtil.isEmpty(mailerLists) || !mailerLists.contains(MailerListType.list_1) || !mailerLists.contains(MailerListType.list_2)) {
            NewsletterMailerList newsletterMailerList1 = new NewsletterMailerList();
            newsletterMailerList1.setMailerListName(MailerListType.list_1);

            NewsletterMailerList newsletterMailerList2 = new NewsletterMailerList();
            newsletterMailerList2.setMailerListName(MailerListType.list_2);

            mailerLists.add(newsletterMailerList1);
            mailerLists.add(newsletterMailerList2);

            newsletter.setMailerLists(mailerLists);
        }


        newsletterService.addNewsletter(newsletter);
        baseResponse.setStatus(StatusType.SUCCESSFUL);
        messages.add("Newsletter added successfully");
        baseResponse.setMessages(messages);
        response = new ResponseEntity<BaseResponse>(baseResponse, HttpStatus.OK);


    }

    return response;


}
}

这是请求类

public class SingleNewsletterRequest {

Newsletter newsletter;

public Newsletter getNewsletter() {
    return newsletter;
}

public void setNewsletter(Newsletter newsletter) {
    this.newsletter = newsletter;
}
}

2 个答案:

答案 0 :(得分:0)

在POST方法中添加消耗属性

@RequestMapping(value = "/newsletters", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<BaseResponse> addNewsletter(RequestEntity<SingleNewsletterRequest> request) {
....
}

这将假定您正在创建的HTTP请求实际上具有

Content-Type:application/json代替text/plain

答案 1 :(得分:0)

在我的情况下,由于DTO的某些功能方法具有遵循Java Beans命名约定的名称而发生了问题,例如isValid()。

在请求json表示中不存在,然后在后端对它进行反序列化时出现了问题。

因此,请尝试检查是否有任何Java Beans方法(get *,set *,is *)在相应的前端对象中没有对应的方法,并避免使用此类方法(我刚刚将名称更改为简单的valid() )。

希望这会有所帮助。