gpt4 book ai didi

java - Spring MVC + Hibernate + JSON post方法保存到数据库

转载 作者:行者123 更新时间:2023-11-30 10:48:54 24 4
gpt4 key购买 nike

我正在编写一个逻辑来将 JSON 数据保存在具有多对多关系( hibernate )的数据库中,为此我有一个 Controller 类、域类、dao 类、pom 和 page.jsp 文件。

这是我正在获取并试图保存在数据库中的 JSON 数据

[{"id":1,"vendor":"6456","venedesc":"PHOA TIAN HUI","venupddat":1450320162470},{"id":2,"vendor":"6827","venedesc":"MKS(ENA O) PUTRA , CV","venupddat":145032016243433240}]

//类用户

 @Entity
@Table(name = "users")
@JsonIgnoreProperties(value = { "role", "enabled", "password" }, ignoreUnknown = true)
public class User {
@Id
@GeneratedValue
private Integer idUser;
private String firstname;
private String lastname;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "USER_VENDOR", joinColumns = { @JoinColumn(name = "idUser") }, inverseJoinColumns = {@JoinColumn(name = "ID") })
private Set<Vendor> vendors = new HashSet<Vendor>();//getter setter

和我的类(class)供应商

 @Entity
public class Vendor {
@Id@GeneratedValue
@Column(name = "ID")
private Integer id;
@Column(name = "vendor")
private String vendor;
//getter setter

Controller

@RequestMapping("/tambah") //to get JSON
public @ResponseBody List<Vendor> getVendor(){
List<Vendor> list = vendorService.getall();
return list;
}

@RequestMapping(value = "/save", method = RequestMethod.POST)//to save
public ModelAndView addUser(@ModelAttribute("user") User user, RedirectAttributes redirectAttributes,Model model) {userService.addUser(user);
ModelAndView modelAndView = new ModelAndView("redirect:/admin/role/user");return modelAndView;}

供应商页面

<form:form commandName="user" action="${pageContext.request.contextPath }/admin/role/user/save" method="post" cssClass="form-horizontal addModalForm">
<div class="modal fade" id="addModalForm" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
<div><div>
<label class="col-sm-2 control-label">Vendor:</label>
<form:select id ="vendorJSON" path="vendors" />

</div></div>
<button type="submit" ><span class="glyphicon glyphicon-ok-circle"></span> Simpan</button>

<script type="text/javascript">
var $select = $('#vendorJSON');
$.getJSON( "${pageContext.request.contextPath }/admin/role/user/tambah", function( data ) {
$select.html('');
$.each(data, function(key, val){
$select.append('<option id="' + val.id + '">' + val.venedesc + '</option>');})
$('#detailVendor').html(data.id);});
$('#addModalForm').modal();

还有我的pom

    jackson-mapper-asl.version 1.9.13 
jackson.version 2.5.4
spring.version 3.2.8.RELEASE
hibernate.version 4.2.8.Final

我不知道发生了什么,因为只显示 HTTP 状态 400

最佳答案

如果您收到错误代码 400,则表示发送的 HTTP 请求有误。无需查看hibernate或数据库,问题发生在这之前。

检查浏览器发送的 HTTP POST 的内容,还检查它发送到的 URL,并确保它们符合 Controller 中配置的内容。

关于java - Spring MVC + Hibernate + JSON post方法保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35620296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com