samedi 5 mai 2018

JSON Need Some Help Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

I'm working on a web project using Java Servlets, this is my first time using AJAX, I'm doing it using the JQuery predefined function, the thing is that I'm sending an object "Company" from the client as a JSON, I'm receiving it on the Servlet the JSON is perfectly parsed and converted to a respective object of the class Company, however doesn't matter if the process go well I am still getting the exception Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

This is my Servlet function:

protected void doRegistryProcessCompany(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{

        try{
            BufferedReader reader = request.getReader(); 
            Gson gson = new Gson();
            Company company = gson.fromJson(reader, Company.class); 

            LoginModel.instance().saveLogin(company.getLogin());
            CompanyModel.instance().saveCompany(company);

            response.setContentType("application/json; charset=UTF-8");
            response.setStatus(204); // ok with no content
        }
        catch(JsonSyntaxException e){
            System.err.println("There was a JSON exception: " + e.getMessage());
            response.setStatus(401); //Bad request
        }
        catch(Exception e){ 
            System.err.println("There was an exception: " + e.getMessage());
            response.setStatus(401); //Bad request
        }   
    } 

I already checked the JSON sent from the client and there is no syntax error:

{
"login": {
    "email": "oracle@gmail.com",
    "type": 1,
    "enable": -128
},
"name": "Oracle",
"description": "Information technology products",
"phoneNumber": "22335524",
"longitude": 3.5323,
"latitude": 2.5667,
"positions": [] }

Also this is my Java "Company class":

public class Company  implements java.io.Serializable {

private Integer idCompany;
private Login login;
private String name;
private String description;
private String phoneNumber;
private Double longitude;
private Double latitude;
private Set<Position> positions = new HashSet<Position>(0);

This is really driving me crazy, I'd appreciate any help.




Aucun commentaire:

Enregistrer un commentaire