Daniele
Daniele

Reputation: 359

Post JSON to Jersey Request

I've spend half a day and got crazy to make my Jersey service accept and manipulate a JSON.

Here is what I'm doing: In PHP using Zend Framework:

$client = new Zend_Http_Client("http://localhost:8080/api/");
    $data = array("city"=> "Paris", "zip" => "1111");
    $json = json_encode($data);     
    $client->setHeaders("Content-type", "application/json");
    $client->setRawData($json, 'application/json')->request("GET");

API method:

@GET
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)   
    public Response getAPI( Address addr) {
        JSONObject out = new JSONObject();
        out.put("city test",addr.getCity());
        Response response = null;
        return response.ok(out.toString()).header("Accept", "application/json").build();
    }   

In a separate file I have my annotated class:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Address
{
    @XmlElement(name="city")
    public String city;
    @XmlElement(name="zip")
    public String zip;

     public String getCity() {
          return city;
     }
}

I get an unsupported media type error:

    Zend_Http_Response Object
(
    [version:protected] => 1.1
    [code:protected] => 415
    [message:protected] => Unsupported Media Type
    [headers:protected] => Array
        (
            [Server] => Apache-Coyote/1.1
            [Content-type] => text/html;charset=utf-8
            [Content-length] => 1117
            [Date] => Tue, 29 May 2012 17:55:03 GMT
            [Connection] => close
        )

    [body:protected] =>  

What am I missing?

Thank you all, Daniele

Upvotes: 1

Views: 662

Answers (1)

Rick Mangi
Rick Mangi

Reputation: 3769

I think you're over complicating this. Since your bean is annotated there's no need to create a json object for it. That's done for you.

return Reponse.ok(addr).build();

Upvotes: 1

Related Questions