weixin_33705053 2013-11-11 19:58 采纳率: 0%
浏览 24

Spring MVC @ResponseBody错误

I have entity class GroupStudent, Spring Controller and JSP page with ajax function. In Controller I try to pass entity GroupStudent object to JSP page using @ResponseBody. But I always get error from browser: Error[object Object]. I found out that i need to add to lib folder in project jackson-core-asl and jackson-mapper-asl jars. I added them (version 1.9.7) and put to spring-servlet.xml so that it automatically could convert GroupStudent object to json format and pass it back to ajax function. But it did not help and I always have the same error dialog in browser. If someone knows how can i pass entity object to ajax using @ResponseBody I will be very gratefull for the help.

Thank you.

GroupStudent class

@Entity
@Table(name = "GroupStudent")
@NamedQueries({ 
@NamedQuery(name = "GroupStudent.getAllGroups", // get all groups
            query = "select g from GroupStudent g"),
@NamedQuery(name = "GroupStudent.getGroupByName", // get group by name
            query = "select g from GroupStudent g where g.groupStudentNumber = :name")
})
public class GroupStudent implements Serializable {
public GroupStudent() {}

public GroupStudent(String groupStudentNumber) {
    this.groupStudentNumber = groupStudentNumber;
}

// create connectivity with table Student
private Set<Student> students = new HashSet<Student>();

@OneToMany(mappedBy = "groupStudent", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Student> getStudents() {
    return this.students;
}   

public void setStudents(Set<Student> students) {
    this.students = students;
}   

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "group_id_seq")
@SequenceGenerator(name = "group_id_seq", sequenceName = "GroupStudent_seq", allocationSize = 1)
@Column(name = "GroupStudentId")
public Long getGroupStudentId() {
    return this.groupStudentId;
}

public void setGroupStudentId(Long groupStudentId) {
    this.groupStudentId = groupStudentId;
}

@Column(name = "GroupStudentNumber")
public String getGroupStudentNumber() {
    return this.groupStudentNumber;
}

public void setGroupStudentNumber(String groupStudentNumber) {
    this.groupStudentNumber = groupStudentNumber;
}

// table GroupStudent fields
private Long groupStudentId;
private String groupStudentNumber;

}

Controller

@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody GroupStudent addNewGroup(@RequestBody GroupStudent group) {

    return group;
}

}

Ajax function

function addGroupAjax() {
            var groupStudentNumber = $('#groupStudentNumber').val();

            $.ajax({
                type: "POST",
                url: "/IRSystem/addData.html",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                mimeType: "application/json",
                data: "{\"groupStudentNumber\":" + "\"" + groupStudentNumber + "\"}",
                success: function(response) {

                },
                error: function(e) {
                    alert("Error" + e);
                }
            });
        } 
  • 写回答

1条回答 默认 最新

  • weixin_33725270 2013-11-11 23:24
    关注

    You could try to add produces="application/json" to Your @RequestMapping so that it looks like @RequestMapping(value = "/addData.html", method = RequestMethod.POST, produces="application/json").

    Also, i didn't see it in the first place, you are missing the @ResponseBody annotation on you POST method. In this blogpost http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html you can see what it does. Short answer: It helps you with serialization/deserialization and writes your serialized object directly to response stream, so you don't have to do it manually.

    Also, check your log on server to see if there are any errors,if not (if you are using Chrome), open Settings -> Tools -> Developer tools -> Network , and fire your call again. Chose the element from the list that shows your call, and there you can see exactly what server returned you.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题