Java jQuery/JSON调用的RESTWS没有响应

Java jQuery/JSON调用的RESTWS没有响应,java,jquery,json,web-services,rest,Java,Jquery,Json,Web Services,Rest,我知道有很多这样的帖子,但我无法真正做到这一点,因为我对REST和JQuery都是新手: 我在Java5中使用REST-WS,我能够调用它,并通过firefox插件“Poster”返回测试结果。当我调用下面的URL时,我应该通过调用下面所示的资源类中的方法“getCustomer”来获取映射中顺序为“0”的员工 虽然我无法获得结果,并且在使用jQuery并返回JSON时,无法获得错误“unknown”,但当我从一个html页面调用其余部分时,正文如下: <body>

我知道有很多这样的帖子,但我无法真正做到这一点,因为我对REST和JQuery都是新手:

我在Java5中使用REST-WS,我能够调用它,并通过firefox插件“Poster”返回测试结果。当我调用下面的URL时,我应该通过调用下面所示的资源类中的方法“getCustomer”来获取映射中顺序为“0”的员工

虽然我无法获得结果,并且在使用jQuery并返回JSON时,无法获得错误“unknown”,但当我从一个html页面调用其余部分时,正文如下:

<body>
        jQuery to REST <br><br>

       <a href="http://jquery.com/">jQuery</a>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
       <script>
         $(document).ready(function(){

            $("button").click(function (){
                $.ajax({
                    type: "GET",
                    url: "http://localhost:8081/RestDemo/services/customers/0",
                    dataType: "json",
                    success: function (data) {
                        alert(data.name);
                    },
                    error: function(e){  
                        alert("Error: " + e);  
                    } 
                });
             });

         });
       </script>
       <br>
       <br>
       <button>Return Customer</button>

      </body>

jQuery到REST

$(文档).ready(函数(){ $(“按钮”)。单击(函数(){ $.ajax({ 键入:“获取”, url:“http://localhost:8081/RestDemo/services/customers/0", 数据类型:“json”, 成功:功能(数据){ 警报(data.name); }, 错误:函数(e){ 警报(“错误:+e”); } }); }); });

退货客户
这是我的资源类:

package com.myeclipseide.ws;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.sun.jersey.spi.resource.Singleton;

@Produces("application/xml")
@Path("customers")
@Singleton
@XmlRootElement(name = "customers")
public class CustomerResource {

    private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();

    public  CustomerResource() {
        // hardcode a single customer into the database for demonstration
        // purposes
        Customer customer = new Customer();
        customer.setName("Harold Abernathy");
        customer.setAddress("Sheffield, UK");
        addCustomer(customer);
    }

    @GET
    @XmlElement(name = "customer")
    public List<Customer> getCustomers() {
        List<Customer> customers = new ArrayList<Customer>();
        customers.addAll(customerMap.values());
        return customers;
    }

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public String getCustomer(@PathParam("id") int cId) {
        return  "{\"name\": \"unknown\", \"address\": -1}"; //customerMap.get(cId); 

    }

    @POST
    @Path("add")
    @Produces("text/html")
    @Consumes("application/xml")
    public String addCustomer(Customer customer) {
         int id = customerMap.size();
         customer.setId(id);
         customerMap.put(id, customer);
         return "Customer " + customer.getName() + " added with Id " + id;
    }

}
package com.myeclipseide.ws;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.TreeMap;
导入javax.ws.rs.Consumes;
导入javax.ws.rs.GET;
导入javax.ws.rs.POST;
导入javax.ws.rs.Path;
导入javax.ws.rs.PathParam;
导入javax.ws.rs.products;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入com.sun.jersey.spi.resource.Singleton;
@生成(“应用程序/xml”)
@路径(“客户”)
@独生子女
@XmlRootElement(name=“客户”)
公共类CustomerResource{
private TreeMap customerMap=new TreeMap();
公共客户资源(){
//将单个客户硬编码到数据库中进行演示
//目的
客户=新客户();
客户名称(“Harold Abernathy”);
客户地址(英国谢菲尔德);
addCustomer(客户);
}
@得到
@xmlement(name=“客户”)
公共列表getCustomers(){
列出客户=新建ArrayList();
customers.addAll(customerMap.values());
返回客户;
}
@得到
@路径(“/{id}”)
@生成(“应用程序/json”)
公共字符串getCustomer(@PathParam(“id”)int-cId){
返回“{\”名称\“:\”未知\“,\”地址\“:-1}”;//customerMap.get(cId);
}
@职位
@路径(“添加”)
@生成(“文本/html”)
@使用(“应用程序/xml”)
公共字符串addCustomer(客户){
int id=customerMap.size();
customer.setId(id);
customerMap.put(id,客户);
返回“Customer”+Customer.getName()+”并添加Id“+Id;
}
}
我感谢任何人的帮助

谢谢

我感谢任何人的帮助

如果你被难倒了,看看网络管理员的日志文件里有什么。如有必要,启用调试日志记录

下一步就是使用web浏览器的内置“web开发者”支持来查看实际发送的请求。

我知道了

返回
{“name”:“unknown”,“address”:-1}
是正确的,因为这正是我的方法return中硬编码的内容

所以我替换了
return“{\”name\:\“unknown\”,“address\:-1}”
简单地用正确的形式

return  "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";
很明显,它是有效的


谢谢大家。

您的HTML页面是从
http://localhost:8081/
也是吗?是的。事实上,它和部署在tomcat上的应用程序是一样的。这是当我用web开发者嗅探请求/响应时得到的结果:响应头Δ49858ms内容类型:application/jsonDate:Tue,2012年7月31日08:12:53 GMTServer:Apache Coyote/1.1传输编码:分块响应体Δ0ms{“name”:“未知”,“地址”:-1}显然,它确实返回了一些东西,但可能是错误的,因为我在返回方法中应用了格式。sp I am sire它正在返回“unknown”,这是在方法getCustomer的返回中定义的名称。现在我只需要在您的数据库中输入一个带有
字符的地址…(请获取一个为您进行引用的库。这样您就永远不会忘记它的重要性!)