Search code examples
spring-bootopenfire

JSON parserError while creating user in Openfire through spring boot application


In my project I have to create a chat box. For that I have to use Open-fire server. I am having the service for creating users in this server. I am facing the problem when I am trying to access the openfire service from my spring boot application. I am have created the model for the user also created the service and provided implementation to it.

This is my model class,

 public class OpenFireUser  {
        private String firstname;
        private String username;
        private String password;
        private String email;

      <----getters and setters--->
        }

This is my service,

public UserCreationResponse createOpenFireUser(String authorization,User createUser) {
        OpenFireUser user= new OpenFireUser();

        user.setEmail(createUser.getEmail());
        user.setFirstname(createUser.getFirstname().toLowerCase());
        user.setPassword("Passw0rd");
        user.setUsername(createUser.getUsername().toLowerCase());

        UserCreationResponse response=new UserCreationResponse();

        RestTemplate restTemplate = new RestTemplate();
        try{
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add("Authorization", authorization);
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json");


        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        HttpEntity<OpenFireUser> requestObject = new HttpEntity<OpenFireUser>(user, headers);
       ResponseEntity<String> responseEntity=restTemplate.postForEntity(OPENFIRE_REST_ENDPOINT, requestObject,String.class);

        int statusCode = responseEntity.getStatusCodeValue();


        if(statusCode==201){
            response.setResponseCode(statusCode);
            return response;
        }else{
            response.setResponseCode(MessageConstant.ResponseCode.ProcessFail.value());} 
            return response;
        }catch(HttpClientErrorException  clientErr){

            response.setUserMessage(clientErr.getMessage());
            response.setResponseCode(clientErr.getStatusCode().value());
            response.setResponseMessage(MessageConstant.CodeMessage.ProcessFail.value());
            return response;
        }
        catch(Exception e)
        {   
            response.setResponseCode(MessageConstant.ResponseCode.ProcessFail.value());
            response.setResponseMessage(MessageConstant.CodeMessage.ProcessFail.value());
            return response;
        }

    }

This is controller code for calling service,

@RequestMapping(value = "/createOpenFireUser", method = RequestMethod.POST, consumes = "application/json;charset=UTF-8",produces = "application/json;charset=UTF-8")
    public UserCreationResponse createOpenFireUser(@RequestBody User createUser) {
        logger.debug("Entering inside createOpenFireUser(@RequestBody OpenFireUser createUser) method");

        //logger.debug("Create user request :  {}" , createUserRequestEntity);
        return userService.createOpenFireUser("authorizationKey",createUser);
    }

while sending data from postman I am getting error like,

 Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of com.exelatech.printshop.model.User out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.exelatech.printshop.model.User out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@6c88daca; line: 1, column: 1]
2018-07-20 15:59:13.535  WARN 9988 --- [nio-9090-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of model.User out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of model.User out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@6c88daca; line: 1, column: 1]

On postman I am getting response like,

"timestamp": 1532082553781,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "JSON parse error: Can not deserialize instance of model.User out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of model.User out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@6c88daca; line: 1, column: 1]",

Can anyone help me to solve my issue?


Solution

  • Error was in sending object through postman. Previously I was sending request object like,

    [
        {
            "firstname" : "Jyoti",
            "username" : "JyotiNH",
            "password":"Passw0rd",
            "email" : "jyoti.kanor@exelaonline.com"
        }
    ]
    

    which is object. But my method receiving parameter as array of strings, So when I sent request with object like following structure, it successfully created the user.

    {
            "firstname" : "Jyoti",
            "username" : "JyotiNH",
            "password":"Passw0rd",
            "email" : "jyoti.kanor@exelaonline.com"
        }