Spring @RequestBody的工作原理
如何获取更多详细信息: 我正在做一个简单的RESTPOST请求,来自邮递员chrome扩展 我的控制器是:Spring @RequestBody的工作原理,spring,spring-mvc,jpa,jpa-2.0,Spring,Spring Mvc,Jpa,Jpa 2.0,如何获取更多详细信息: 我正在做一个简单的RESTPOST请求,来自邮递员chrome扩展 我的控制器是: @Controller @RequestMapping("/theme") public class ThemeController { @RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public @R
@Controller
@RequestMapping("/theme")
public class ThemeController {
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
Status addTheme(@RequestBody Theme theme) {
try {
themeServices.addEntity(theme);
return new Status(1, "Theme added Successfully !");
} catch (Exception e) {
// e.printStackTrace();
return new Status(0, e.toString());
}
}
在Theme.java中:
@Entity
@Table(name = "theme", uniqueConstraints = { @UniqueConstraint(columnNames = { "theme_id" }) })
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@NamedQuery(name = "Theme.findAll", query = "SELECT t FROM Theme t")
public class Theme implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "theme_id")
private long themeId;
private String description;
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "category_id", nullable=true)
private ThemeCategory themeCategory;
在mecategory.java中:
@Entity
@Table(name = "theme_category", uniqueConstraints = { @UniqueConstraint(columnNames = { "category_id" }) })
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@NamedQuery(name = "ThemeCategory.findAll", query = "SELECT t FROM ThemeCategory t")
public class ThemeCategory implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "category_id")
private long categoryId;
private String description;
private String name;
// bi-directional many-to-one association to Theme
// @OneToMany(mappedBy="themeCategory")
@OneToMany(mappedBy = "themeCategory", fetch = FetchType.EAGER)
@Column(nullable = true)
@JsonManagedReference
private Set<Theme> themes;
// bi-directional many-to-one association to ThemeCategory
@ManyToOne
@JoinColumn(name = "parent_category_id", nullable=true)
@JsonBackReference
private ThemeCategory parentThemeCategory;
// bi-directional many-to-one association to ThemeCategory
// @OneToMany(mappedBy="themeCategory")
@OneToMany(mappedBy = "parentThemeCategory", fetch = FetchType.EAGER)
@Column(nullable = true)
@JsonManagedReference
private Set<ThemeCategory> themeCategories;
主题表:
CREATE TABLE `theme` (
`theme_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`category_id` smallint(5) unsigned NOT NULL,
`file_path` varchar(200) DEFAULT NULL,
`description` varchar(1000) DEFAULT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`theme_id`),
KEY `idx_category_id` (`category_id`),
CONSTRAINT `fk_category_id` FOREIGN KEY (`category_id`) REFERENCES `theme_category` (`category_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=401 DEFAULT CHARSET=utf8;
我正在使用邮差分机拨打rest post电话:
标题参数:
内容类型:application/json
Json正文:
{"description":"theme8","name":"theme8","themeCategory":{"categoryId":302, "themes":[],"parentThemeCategory":{}, "themeCategories":[]}}
并尝试了2个小时左右的多种身体方式。但它一直在说:
服务器拒绝了此请求,因为请求实体的格式不受支持
由请求的方法的请求资源创建
分析一下,我没有得到任何其他东西。在Eclipse控制台中,也没有显示与此问题相关的任何内容
怎么了?是否有任何工具可以创建有效的请求。我尝试了您的代码,使用了相同的请求,效果良好。唯一的区别是,我使用“高级Rest客户端”发送请求。在类别和主题实体之间有一个主题\类别表。其中维护了主题类别和主题。这可能是我发布所有代码的原因。请查看它。为application/json添加一个Accept头,然后重试
{"description":"theme8","name":"theme8","themeCategory":{"categoryId":302, "themes":[],"parentThemeCategory":{}, "themeCategories":[]}}