无法在Java中检索restfulwebservice发送的数据

无法在Java中检索restfulwebservice发送的数据,java,ajax,json,eclipse,rest,Java,Ajax,Json,Eclipse,Rest,我通过restfulwebservice从Javascript发送Json作为post请求,但在Java文件中,断点从未中断,该文件是post类型,并且使用application/Json HTML: 爪哇: 我得到了解决方案: 问题: xmlhttp.send(empJson); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4){ var

我通过restfulwebservice从Javascript发送Json作为post请求,但在Java文件中,断点从未中断,该文件是post类型,并且使用application/Json

HTML:

爪哇:

我得到了解决方案:

问题:

    xmlhttp.send(empJson);
    xmlhttp.onreadystatechange = function() {               
   if(xmlhttp.readyState == 4){
        var result= xmlhttp.responseText;           
    }
    } 
解决方案:

xmlhttp.send(JSON.stringify(empJson));
xmlhttp.onreadystatechange = function() {               
        if(xmlhttp.readyState == 4){
            var result= JSON.parse(xmlhttp.responseText);           
        }
        }
解释:

我需要将其转换为字符串以作为Json传递,并使用Jackson库将其直接映射到类,在接收它时,我将其作为文本接收,并使用Json.parse解析为对象。

您是否收到任何错误(如未找到404)?我怀疑Web服务可能尚未发布。打开浏览器调试器,检查调用服务时得到的响应。同时发布您的
web.xml
。请求映射涉及多个级别。文件
web.xml
如前一条注释中所强调的,在资源类中注释
@Path
,最终在应用程序类中注释
@ApplicationPath
。如果没有这些元素,我们就帮不了你。谢谢@PramodKarandikar当我发送字符串并将consumes设置为text/plain时,我尝试调试它,但如果我更改为json,它不会调用函数,当我从rest客户端发布json时,它给出了415个错误当您实际发布AJAX json请求时,看到的内容类型是什么?签入浏览器调试器。可能,它总是以
文本/纯文本的形式运行。
    xmlhttp.send(empJson);
    xmlhttp.onreadystatechange = function() {               
   if(xmlhttp.readyState == 4){
        var result= xmlhttp.responseText;           
    }
    } 
xmlhttp.send(JSON.stringify(empJson));
xmlhttp.onreadystatechange = function() {               
        if(xmlhttp.readyState == 4){
            var result= JSON.parse(xmlhttp.responseText);           
        }
        }