JSON解析错误:无法构造``no String-argument constructor / factory方法的实例从String值反序列化

时间:2018-04-23 04:08:13

标签: java rest spring-boot resttemplate

我无法在下面的代码中找出我做错的事情,以使客户端使用其余服务。 我想发送json而不是person对象,并以相同的方式返回响应。

我的来电服务如下:

@Component
public class CallRestService implements CommandLineRunner {

@Autowired
private RestTemplate restTemplate;

private void callRestService() {

    // create a request body
    String requestBody = "{\"id\":\"1\",\"firstName\":\"replacedFirstName\",\"lastName\":\"replacedLastName\", \"age\":30}";

    // set headers 
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON   );
    HttpEntity<String> entity = new HttpEntity<String>(requestBody, headers);

    //send the request
    Object response1 = restTemplate.postForObject("http://localhost:8080/person/1", entity, Object.class);
    System.out.println("Person's details are: " + response1);

}

@Override
public void run(String... args) throws Exception {  
    callRestService();  

}
}

我消耗的休息服务控制器如下:

@RestController
@RequestMapping("/person")
public class PersonController {


@Autowired
PersonService personService;

@GetMapping("/all")
public Hashtable<String, Person> getAll(){
    return personService.getAll();
}

@GetMapping("/{id}")
public Person getPerson(@PathVariable("id") String id) {
    return personService.getPerson(id);
}

@PostMapping("/{id}")
public Person setPerson(@PathVariable("id") String id, @RequestBody Person person) {

    Person updatedPerson = personService.getPerson(id);
    updatedPerson.setFirstName(person.getFirstName());
    updatedPerson.setLastName(person.getLastName());

    return updatedPerson;
}

}

我服务的人员课程如下:

public class Person {

String id;
String firstName;
String lastName;
int age;

...// getters and setters
}

任何人都可以告诉我,我为犯这个错误错误做了什么错误吗?

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.rest.model.Person` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":"1","firstName":"replacedFirstName","lastName":"replacedLastName", "age":30}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.rest.model.Person` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":"1","firstName":"replacedFirstName","lastName":"replacedLastName", "age":30}') at [Source: (PushbackInputStream); line: 1, column: 1]

EDITED 这是我使用mapper的restTemplate配置:

@Configuration
public class RestConfiguration {

@Autowired
private ObjectMapper objectMapper;

@Bean
public RestTemplate createRestTemplate() { 
 objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(objectMapper);
    messageConverters.add(jsonMessageConverter);
    restTemplate.setMessageConverters(messageConverters);
    return restTemplate;
}   
}

0 个答案:

没有答案