在Spring Boot中使用JSON HttpMessageConverters

时间:2018-01-05 05:14:08

标签: java json spring spring-mvc spring-boot

我在尝试调用API时尝试使用HTTPMessageConverters将JSON输入转换为对象。但是,当我尝试将API输入映射到POJO时,我看到了一条错误消息。鉴于错误消息,我认为该问题与CreateUserRequest类实现有关。 任何建议将不胜感激!请参阅以下内容:

错误消息:

{
"timestamp": 1515127031483,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Cannot construct instance of `com.api.service.request.CreateUserRequest`, problem: email\n at [Source: (PushbackInputStream); line: 6, column: 1]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.api.service.request.CreateUserRequest`, problem: email\n at [Source: (PushbackInputStream); line: 6, column: 1]",
"path": "/create"
}

API输入:

curl -X PUT \
http://localhost:8080/create \
-H 'authorization: Basic Ymx1bWV3OjEyMw==' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
    "CreateUserRequest" : {
        "email" : "--REDACTED--",
        "password" : "--REDACTED--"
    }
}'

API控制器:

@RestController
public class BaseActivity {

@RequestMapping(path = "/create", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody String createUser (final @RequestBody CreateUserRequest createUserRequest) { ... }

WebConfig:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"com.api.service"})
public class ActivityConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(
        List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
        super.configureMessageConverters(converters);
    }

    @Bean
    public MappingJackson2HttpMessageConverter 
        mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new 
            MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper() 
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }
}

请求对象:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateUserRequest implements Serializable {

    private static final long serialVersionUID = 4260711945094777831L;

    @NonNull
    private String email;

    @NonNull
    private String password;
}

注意:当我从createUserRequest类替换lombok注释时,将调用API但电子邮件/密码属性为null。

1 个答案:

答案 0 :(得分:1)

你的RequestBody是这样的

with both as (select * from t1 union select * from t2),
--- col1,col2,col3 for which one is null and the other is not
     inter as ((select col1, col2, col3 from t1 where algo is null 
               intersect
               select col1, col2, col3 from t2 where algo is not null) 
               UNION
               (select col1, col2, col3 from t1 where algo is not null 
               intersect
               select col1, col2, col3 from t2 where algo is null
select * from both
  except
-- remove those that are in the intersection but also have null
select * from 
    (select * from both where algo is null)  as temp2
    natural join inter;

但我认为应该如下:

{
    "CreateUserRequest" : {
        "email" : "--REDACTED--",
        "password" : "--REDACTED--"
    }
}