具有适当内容类型的HTTP GET请求未达到预期的服务方法

时间:2015-11-16 13:15:15

标签: web-services rest get jersey conflict

我在类CustomerResource中有2个restful服务方法getCustomerJson和getCustomerXML,我在其中使用针对Restful Webservices的jersey API。两个方法的所有参数都是相同的,除了一个产生xml而另一个产生json。

当我使用带有标题Content-Type =“application / json”的HTTP GET请求时,它总是调用返回xml的getCustomerXML方法。

有人能解释一下球衣在这种情况下是如何运作的吗?

import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import domain.Customer;

@Path("/customers")
public class CustomerResource {
    private static Map<Integer, Customer> customerDB = new ConcurrentHashMap<Integer, Customer>();
    private static AtomicInteger idCounter = new AtomicInteger();

    // Constructor
    public CustomerResource() {
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello Kundan !!!";
    }



    @GET
    @Path("{id}")
    @Produces("application/xml")
    public Customer getCustomerXML(@PathParam("id") int id, @Context HttpHeaders header) {
        final Customer customer = customerDB.get(id);
        List<String> contentList = header.getRequestHeader("Content-Type");
        List<String> languageList = header.getRequestHeader("Accept-Language");
        List<String> compressionFormatList = header.getRequestHeader("Content-Type");
        if (customer == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        return customer;
    }

    @GET
    @Path("{id}")
    @Produces("application/json")
    public Customer getCustomerJson(@PathParam("id") int id) {
        final Customer customer = customerDB.get(id);
        if (customer == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        return customer;
    }

    @POST
    @Consumes("application/xml")
    public Response createCustomer(Customer customer) {
        customer.setId(idCounter.incrementAndGet());
        customerDB.put(customer.getId(), customer);
        System.out.println("Created customer " + customer.getId());
        return Response.created(URI.create("/customers/" + customer.getId())).build();

    }

    @PUT
    @Path("{id}")
    @Consumes("application/xml")
    public void updateCustomer(@PathParam("id") int id, Customer customer) {
        Customer current = customerDB.get(id);
        if (current == null)
            throw new WebApplicationException(Response.Status.NOT_FOUND);

        current.setFirstName(customer.getFirstName());
        current.setLastName(customer.getLastName());
        current.setStreet(customer.getStreet());
        current.setCity(customer.getCity());
        current.setState(customer.getState());
        current.setZip(customer.getZip());
        current.setCountry(customer.getCountry());

    }

    @DELETE
    @Path("{id}")
    public void deleteCustomer(@PathParam("id") int id) {
        customerDB.remove(id);
        System.out.println("Deleted !");

    }
}

1 个答案:

答案 0 :(得分:1)

使用Accept: application/jsonAccept告诉服务器您想要返回的类型。 Content-Type如果是发送到服务器的数据类型,就像POST请求一样。