从宁静的Web服务发送和接收JSON数据?

时间:2014-03-17 12:35:36

标签: java json web-services rest

这是我的POJO代码:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.akapoor.ws.testws.model;

import java.io.File;
import java.io.IOException;
import javax.annotation.ManagedBean;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response;  
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONObject;

/**
*
* @author
*/
@XmlRootElement(name = "person")
@XmlType(propOrder = {"id", "fullName", "age"})
public class Person{

private int id;
private String fullName;
private int age;

//Must have no-argument constructor
public Person() {
}

public Person(String jsonRepresentation) {
     //Converts Java Object in & From Json
    ObjectMapper mapper = new ObjectMapper();//Jackson
    Person object; 

    try {

        /*Deserialisierung
         * JSON Unmarshalling (FROM JSONrepresentation to Object)
         * Converting JSON String 
         */ 
        object = mapper.readValue(jsonRepresentation, Person.class); //Jackson JSON

    } catch (IOException e) {
        throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
                entity("Couldn´t parse JSON string: " + e.getMessage()).build());
    }
    this.age = object.getAge();
    this.fullName = object.getFullName();
    this.id = object.getId();
}

/* toPack from String to Object
 * Deserializes an Object of class Person from its JSON representation+
 * Constructor with String Json
 */
// JSON to Person 
public static Person fromString (String jsonRepresentation) {

    //Converts Java Object in & From Json
    ObjectMapper mapper = new ObjectMapper();//Jackson
    Person object; 

    try {
        /*Deserialisierung
         * JSON Unmarshalling (FROM JSONrepresentation to Object)
         * Converting JSON String 
         */ 
        object = mapper.readValue(jsonRepresentation, Person.class); //Jackson JSON

    } catch (IOException e) {
        throw new WebApplicationException(Response.status(Status.BAD_REQUEST).
                entity("Couldn´t parse JSON string: " + e.getMessage()).build());
    }
    return object; 
}

// Getter & Setter
@XmlElement
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@XmlElement
public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

@XmlElement
public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}

我的服务看起来像这样:

@Path("service")
public class Service {
//GetObject
@POST
@Path("/getObject")
//JSON-Serialisierung --> JSON-serialisiertes Objekt wird vom Browser empfangen
@Produces(MediaType.APPLICATION_JSON) 
//JSON-Deserialisierung
@Consumes(MediaType.APPLICATION_JSON)  //client sends also JSON
public Person getObjectWithObject(@QueryParam("person") Person person) {

     //Encode a JSON object
    //{"id":1,"fullName":"name","age":22}

    String fullName = person.getFullName();
    int age = person.getAge()``;
    int id = person.getId();

    //Encode a JSON object

    //{"id":1,"fullName":"name","age":22}


    //return Object with details
    return person;
    //return person; 
}
} //End of class Service

如何为我的Web服务编码解码JSON。

我正在使用JAX-RS。并希望在浏览器中使用以下URI接收我的JSONRespresentation:

Ex: 我的URI怎么样?

帮助我使用@Produces和@Consumes

重新呈现JSON

1 个答案:

答案 0 :(得分:1)

不要使用@QueryParam,只需提供您的POJO课程:

@POST
@Path("person")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Person createPerson(Person person) {
    //Process retrieved data
    String name = person.getName();
    return person;
}

编组和解组JSON数据将由您的框架(例如Jersey)处理。