简体   繁体   中英

Handle Blob data via Ajax call in spring MVC

I need to handle Blob data via jquery Ajax call in spring MVC.

I am working on banking application where i have web page in which I want to send ajax request to retrieve all the client details. But the problem is that, in my Pojo I have blob datatype for the clientImage, which I'm not able to handle.

public class Client {

 @Id
 @GeneratedValue
 private Long clientId;
 private String clientTitle;
 private int clientAge;
 private int clientRetirementAge;
 private int clientLifeExpectancy;
 private String clientFirstName;
 private String clientMiddleName;
 private String clientLastName;
 private String clientNotes;
 private String clientGender;
 private String clientDOB;
 private String clientJoint;
 private String clientEmail;
 private String clientPhoneNo;
 private String riskTolerance;
 private String status; // --- Active or notActive
 private String password;
 private String partnerAvailibility; // -- Yes or No
 private String relationshipWithPartner;

 private Blob clientImage;
    @Column(name="filename")
    private String filename;  
    @Column(name="content_type")
    private String contentType;

  // =====Client Attributes Block End=====================//
//getter and setter
....
}//end

and see my ajax request....

$.ajax({
            type: "POST",
            url: "showClientDetails.html",
            data: "clientId=" + clientId,
            success: function(response){
             //console.log(response);
 var title=response.clientTitle,firstName=response.clientFirstName,lastName=response.clientLastName,DOB=response.clientDOB,phone=response.clientPhoneNo,email=response.clientEmail,partnerAvailibility=response.partnerAvailibility;


             $("#personal-info-title-label").html(title);
             $("#personal-info-firstName-label").html(firstName);
             $("#personal-info-lastName-label").html(lastName);

             $("#personal-info-dob-label").html(DOB);
             $("#personal-info-phone-label").html(mp);
             $("#personal-info-email-label").html(email);


            },
            error: function(e){
            alert('Error: ' + e.responseText);
            }
            });

and finally see my Spring Controller.....

@RequestMapping(value="/showClientDetails",method=RequestMethod.POST)
 public @ResponseBody Client showClientDetails(HttpServletRequest request,Client client) {
  //System.out.println("in show clients");
  Long clientId=Long.parseLong(request.getParameter("clientId"));
  client=userService.getClientDetails(clientId);
  return client;
 }

I need to handle this via ajax request only. So help me if u have any solution or provide me with any link if you know.

I worked on an app once that had image data stored in a table, and used AJAX in a similar way. Our approach was to not send the blob to the view, but rather create a controller for viewing the images, and as part of the domain object we send to the view generate the URL that points to our Image Controller. On the view-side (we were using JSPs, if it matters) we simple used an image tag and pumped in the generated URL.

On the Image Controller, we had a method that used @ResponseBody, returning a byte[]. We selected the data from the table, set the Content Type in the headers, and returned the byte[].

Since I wrote that code (several years ago), we have had other instances where we had to return file data, like Excel spreadsheets. Recently, however, I have taken the approach of not using @ResponseBody, but rather create a custom View (extending AbstractView ) and returning that as part of a ModelAndView . I think this is a lot cleaner approach than simply telling Spring to shove your output into the HTML response body, which is what @RequestBody is doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM