从宁静的Web服务发送和接收JSON数据? [英] Sending and Receiving JSON data from a restful webservice ?

查看:105
本文介绍了从宁静的Web服务发送和接收JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的POJO代码:

Here is my POJO Code :

/*
* 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.

How I can encode an decode JSON for my Webservice.

我正在使用JAX-RS.并希望在浏览器中使用URI接收我的JSONRespresentation,例如:

I am Using JAX-RS. and want to receive my JSONRespresentation in Browser with a URI like:

例如: 我的URI看起来如何

Ex : How will be my URI look like

帮助我使用@Produces和@Consumes表示JSON

Help me to respresent JSON using @Produces and @Consumes

推荐答案

不要使用@QueryParam,只需提供如下的POJO类即可:

Don't use @QueryParam, just provide your POJO class like this:

@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)处理.

Marshalling and unmarshalling JSON data will be handled by your framework (e.g. Jersey).

这篇关于从宁静的Web服务发送和接收JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆