我正在使用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;
}
}
在您的POST方法中添加消耗属性
@RequestMapping(value = "/newsletters", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<BaseResponse> addNewsletter(RequestEntity<SingleNewsletterRequest> request) {
....
}
这将假设您正在创建的HTTP请求实际上具有
内容类型:application/json
而不是text/plain
在我的例子中,出现这个问题的原因是dt的一些函数方法的名称遵循javabean命名约定,例如isValid()。
它在请求json表示中不存在,然后,当它在后端反序列化时,问题发生了。
因此,请尝试检查是否有任何Java Beans方法(get*、set*、is*)在各自的前端对象中没有对应的方法,并避免使用此类方法(我刚刚将名称更改为simply valid())。
希望这有所帮助。
当我在localhost:8080/api/users上做一个POST请求来创建一个新用户时,我会得到以下错误: 是请求的主体,选择JSON(应用程序/JSON)。即使我删除角色并将其保持为空,它也会给出相同的错误。 标题的内容类型也是application/json。 这是我的控制器: UserService中的createUser函数: 这是我的用户类:
嗨,有人能帮我处理这个错误吗?当我使用邮递员发送邮件请求时,这里是我的控制器 这就是我使用postman发送json的方式 我正在尝试搜索如何修复它,但错误仍然存在 编辑:这是邮递员的标题 提前致谢
当我从jQuery向Spring RestController发送JSON包时,我有很多错误: 在Spring: 已解决[org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application/json;字符集=UTF-8'不受支持] 在Chrome: 邮政http://localhost/post415 (匿名)@m
基于用Spring@Controller编码的x-www-form-Url问题的答案 我写了下面的@Controller方法 以下错误导致请求失败 [附言:泽西岛要友好得多,但考虑到这里的实际限制,现在不能使用它]
问题内容: 使用firebug时,我在asp.net mvc 4项目中收到此有线错误“ NetworkError:415无法处理… xt / xml; charset = utf-8’– ”。 代码: 服务代码: 该接口是: 和网络配置,我在WCF中使用了无文件: 问题答案: 您需要使用,而不是代码中的常规。这将为端点配置适当的绑定()和行为()以遵守该属性。
我的角码 你有什么想法吗?