JSON 通过 spring 引导应用程序在 Openfire 中创建用户时出现 parserError

JSON parserError while creating user in Openfire through spring boot application

在我的项目中,我必须创建一个聊天框。为此,我必须使用 Open-fire 服务器。我有在此服务器中创建用户的服务。 当我尝试从我的 spring 启动应用程序访问 openfire 服务时,我遇到了问题。 我为用户创建了模型,还创建了服务并为其提供了实现。

这是我的模型class,

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

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

这是我的服务,

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;
        }

    }

这是调用服务的控制器代码,

@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);
    }

从邮递员发送数据时出现错误,如

 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]

在邮递员上,我收到了这样的回复,

"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]",

谁能帮我解决问题?

通过邮递员发送对象时出错。 以前我发送的请求对象是

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

这是对象。 但是我的方法接收参数作为字符串数组, 所以当我发送带有如下结构的对象的请求时,它成功地创建了用户。

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