2016-12-14 95 views
0

如何检查对servlet的请求是否是JSON请求?检查JSON请求与否

如果请求是JSON而不是JSON中的响应。否则,它将成为网络请求,响应将成为网络响应。

+0

你的问题是无法理解的。尝试并将其编辑为可以理解和回答的问题。 – jwpfox

+0

“Content-Type”头应该规定正文的内容,但是“Accept”头应该规定客户端支持的内容。客户端可以传递'application/json'的数据,但在Accept中需要'application/xml'。 –

回答

0

您可以获取Content-Type http头(由客户端发送)并检查它是否为'application/json'。

if ('application/json'.equals(request.getHeader('content-type')) { 
    // send json response 
} else { 
    // send web response 
} 

其中requestHttpServletRequest