我正在创建一个使用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;
}
}
我给予包含类型为应用程序/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;
}
}
3条答案
按热度按时间eivnm1vs1#
在我的例子中,发生问题的原因是DTO的一些函数方法具有遵循Java Bean命名约定的名称,例如isValid()。
请求json-representation中不存在该属性,然后在后端对其进行反序列化时出现问题。
因此,尝试检查是否有任何Java Bean方法(get*、set*、is*)在各自的前端对象中没有对应的方法,并避免使用此类方法(我只是将名称更改为valid())。
希望这能帮上忙。
8tntrjer2#
在POST方法中添加consumes属性
这将假定您正在创建的HTTP请求实际上具有
Content-Type:application/json
而不是text/plain
tv6aics13#
这里回答道:https://stackoverflow.com/a/50567626/8956733
尝试删除
@JsonManagedReference
有点奇怪,但它确实帮助了我以及(见我的评论如下)。