webservice ready model not atribute使用@RequestBody

时间:2016-08-19 12:24:35

标签: java json spring

当属性的ready属性准备就绪时,我们的问题在于带有模型的ready对象。

  • 使用spring 4.2.4-RELEASE的项目
  • 用户jackson 2.8.0

描述=>

我们做什么,我们在Spring的RestController中做一个web服务,我们必须接收HTTP请求 POST 方法,内容类型应用程序/ x-www-form-urlencoded 直到现在我们准备了带参数的对象这个工作 @RequestParam(“event”)字符串事件,但该对象有很多参数,对象调用数据有很多属性。如果没有它运行,我们需要为方法编写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]

我们需要准备一个模型对象,而不是属性

1 个答案:

答案 0 :(得分:0)

我可以看到异常说你的json有问题。您需要找到实际正在解析的JSON。在调试器中运行应用程序,在JsonParseException的相关构造函数上设置断点...然后找出它试图解析的ByteArrayInputStream中的内容。