【问题标题】:webservice ready model not attribute use @RequestBody网络服务就绪模型不使用 @RequestBody 属性
【发布时间】: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!能否在您努力解决问题的同时,在内容中提供更好的标题和更详细的信息?

标签: java json spring


【解决方案1】:

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

【讨论】:

  • 是的,同样的错误解析,但我不知道解决了这个问题,我们做了很多配置,当方法调用中没有参数到达时,如果输入参数@requestBody,sametimesgenerete erros other not .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 2020-04-30
  • 1970-01-01
  • 2021-05-14
  • 2017-03-14
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多