当前位置: 首页 > 知识库问答 >
问题:

内容类型'应用程序/json; charset=UTF-8'不支持

长孙和悦
2023-03-14

我正在使用JavaSpring框架创建一个时事通讯API。每当我以请求模型的帖子的形式访问API时,我都会得到这个例外组织。springframework。网状物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;
}
}

我给包含类型作为应用程序/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个答案

法弘壮
2023-03-14

在您的POST方法中添加消耗属性

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

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

内容类型:application/json而不是text/plain

屈畅
2023-03-14

在我的例子中,出现这个问题的原因是dt的一些函数方法的名称遵循javabean命名约定,例如isValid()。

它在请求json表示中不存在,然后,当它在后端反序列化时,问题发生了。

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

希望这有所帮助。

 类似资料: