Reputation: 2517
I know there is a lot of these posts but I couldn't really get this to work since I am new to both REST and JQuery:
I am using REST-WS with Java 5 and I am able to call it and get result back with "Poster" ,the firefox plugin to test it. When I call the URL below I should get the employee in order '0' in the map by calling the method "getCustomer" in the resource class shown below.
Although I am not able to get the result and getting an error "unknown "using jQuery and returning JSON when I call the REST from an html page with body as below:
<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>
This is my Resource class:
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;
}
}
I appreciate anyone's help,
Thanks!
Upvotes: 2
Views: 1098
Reputation: 2517
I got it!
Returning {"name": "unknown", "address": -1}
is right because that's exactly what is hard coded in my method return,
so i replaced return "{\"name\": \"unknown\", \"address\": -1}";
simply with a correct form which is
return "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";
and obviously it works!
Thanks everyone.
Upvotes: 1
Reputation: 719446
I appreciate anyone's help,
If you are stumped, take a look at what is in the web countainer's log files. If necessary, turn on debug logging.
The next thing would be to use your web browser's built-in "web developer" support to see what request is actually being sent.
Upvotes: 0