9

I'm trying to get a list of objects to render on a Spring 3 MVC app and would like to do this via Ajax.

So in my Spring class I have:

@RequestMapping(value = "/viewSearchEnquiriesAjax", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<Enquiry> getEnquiriesBySearchAjax(@RequestParam String name) {
    Search search =  new Search();
    search.setFirstName(name);
    return searchEnquiries(search);
}

But I get a 500 (Internal Server Error) when this is run. This manifests itself when I'm debugging in the browser as 'GET http://localhost:8080/SpringMVC/viewSearchEnquiriesAjax?name=peter 500 (Internal Server Error)'

I can successfully return a single object with no error. Can the Spring Json mapper(Jackson) convert correctly? Am I missing something fundamental?

My javascript is as follows:

function doAjaxPost() {
// get the form values
var firstName = $('#firstName').val();
$.getJSON("/SpringMVC/viewSearchEnquiriesAjax", { name: firstName }, function(result) {
    alert("Success");
 });

}

My Enquiry object is an Entity:

@Entity
@Table(name = "enquiries")
public class Enquiry implements java.io.Serializable{

    private static final long serialVersionUID = -5093725544297637792L;

    protected Long id;
    protected Date created = new Date();
    ...
    ...

    public Enquiry() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }
   ...
   ...
enkor
  • 7,527
  • 3
  • 31
  • 55
  • How does your `Enquiry` / `Search` class look like? – Grzegorz Rożniecki Oct 05 '12 at 12:44
  • I have edited my question to include my Enquiry class but have truncated the code. The Search class is simply a wrapper to I can do the search, the search method could be overloaded to accept a String. – enkor Oct 05 '12 at 13:04
  • please post the relevant error. – G-Man Oct 05 '12 at 13:04
  • error posted into the question, I'm getting it in the browser console. – enkor Oct 05 '12 at 13:09
  • i mean on the java side of things, there should be a stacktrace in the server logs – G-Man Oct 05 '12 at 13:10
  • Nothing, there is no stack trace or error being generated. – enkor Oct 05 '12 at 13:17
  • check out the body of the 500 error page, maybe it contains more information. there realy has to be more then a 500 – G-Man Oct 05 '12 at 13:24
  • assuming you use chrome cause the error so it's in console, press the network tab, click the red entry and press preview in the left pane at the top, it should show a html page with a stack trace in there. – G-Man Oct 05 '12 at 13:36
  • Boom! found the error. I ended up writing some code to parse the Object with Jackson and tracked it down to the errors I was getting. Found there was a getter method conflict in the Enquiry class. But thanks G-Man for your help, I needed that nudge to think about getting an error out. – enkor Oct 05 '12 at 13:40

1 Answers1

9

For Jackson you have to create a strongly typed list class because of type erasure:

public class EnquiryList extends ArrayList<Enquiry> {
}

Then return this list from your controller:

@RequestMapping(value = "/viewSearchEnquiriesAjax", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody EnquiryList getEnquiriesBySearchAjax(@RequestParam String name) {
    EnquiryList list = new EnquiryList();
    ...
    return list;
}

Also check out this answer on a similar question.

Community
  • 1
  • 1
James
  • 11,654
  • 6
  • 52
  • 81