【发布时间】:2016-12-26 14:41:27
【问题描述】:
我们的问题在于带有模型的就绪对象,当属性的就绪属性可以就绪时。
- spring 4.2.4-RELEASE 项目
- 用户jackson 2.8.0
描述 =>
我们做什么,我们基于Spring的RestController做一个webservice,我们必须接收HTTP请求 POST 方法,content-type application / x-www-form-urlencoded 直到现在我们通过参数准备对象这个工作 @RequestParam("event") String事件,但是对象有很多参数,对象调用数据有很多属性。如果没有它运行,我们需要为方法编写一个 5 行平均代码。如果工作 @RequestBody 有一个参数,它非常快。
**webservice rest server**
@RestController
@RequestMapping(value = /invoice)
public class Invoice
{
//invoice create
@RequestMapping(value = /{id}/create, method = RequestMethod.POST)
@Produces(value = { MediaType.TEXT_HTML_VALUE })
@Consumes(value = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public @ResponseBody String createInvoice(@PathVariable(value = "id") String id,
@RequestBody Notification notificationData)
{
return "OK";
}
//change state invoice, first state
@RequestMapping(value = /{id}/change, method = RequestMethod.POST)
@Produces(value = { MediaType.TEXT_HTML_VALUE })
@Consumes(value = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public String changeState(@PathVariable(value = id) String id, @RequestParam("event") String event,
@RequestParam ("data[id]") String dataId, @RequestParam("data[account_id]") String accout)
{
// call arrived here, with value of variable
return "OK";
}
//change state invoice, second state
@RequestMapping(value = /{id}/change, method = RequestMethod.POST)
@Produces(value = { MediaType.TEXT_HTML_VALUE })
@Consumes(value = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public String changeState(@PathVariable(value = id) String id, @RequestBody Notification notificationData)
{
// not arrived here
return "OK";
}
}
**model**
@JsonIgnoreProperties(ignoreUnknown = true)
public class Notification implements Serializable
{
private static final long serialVersionUID = 1L;
@JsonProperty("data")
private Data data;
@JsonProperty("event")
private String event;
}
**child model**
@JsonIgnoreProperties(ignoreUnknown = true)
public class Data implements Serializable
{
private static final long serialVersionUID = 1L;
@JsonProperty("id")
private String id;
@JsonProperty("account_id")
private String account;
}
**part of converter**
@Configuration(value = "webapp")
@EnableWebMvc
public class SysWebAppInitializer extends WebMvcConfigurerAdapter
implements WebApplicationInitializer{
private static final Charset UTF8 = Charset.forName("UTF-8");
private static final List<MediaType> mediaType =
Arrays.asList(new MediaType("application", "json", UTF8),
new MediaType("text", "plain", UTF8), new MediaType("text", "html", UTF8),
new MediaType("multipart", "form-data", UTF8),
new MediaType("application", "x-www-form-urlencoded",UTF8));
@Bean(name = "jackson2Converter")
@Scope("prototype")
public MappingJackson2HttpMessageConverter jackson2Converter()
{
final MappingJackson2HttpMessageConverter jackson2Converter =
new MappingJackson2HttpMessageConverter();
jackson2Converter.setSupportedMediaTypes(mediaType);
jackson2Converter.setObjectMapper(new ObjectMapper().
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
return jackson2Converter;
}
@Override
public void configureMessageConverters(final List<HttpMessageConverter<?>> converters)
{
converters.addAll(this.buildListConverters(mappingJackson2HttpMessageConverter()));
}
}
**same stacktrace wrong, same call method create**
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'event': was expecting ('true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream@4efc31a9; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'event': was expecting ('true', 'false' or 'null')
at [Source: java.io.ByteArrayInputStream@4efc31a9; line: 1, column: 7]
我们需要准备一个模型对象,而不是属性
【问题讨论】:
-
这里需要更多信息。您尝试发布的 Json 文档是什么样的?这个 Controller 方法的方法签名是什么样的,最后,您尝试将此数据绑定到什么 POJO?
-
抱歉,我正在编辑并在完成之前发送
-
仍然需要失败的请求正文。它是 JSON 文档还是表单数据?此外,您没有发布
Notification中引用的Data类 -
你,发代码,调用数据
-
欢迎来到 Stack Overflow!能否在您努力解决问题的同时,在内容中提供更好的标题和更详细的信息?