2015-08-03 4 views
1

J'ai une représentation en casse de serpent des données json '{"first_name": "Michael", "last_name": "Jordan", "address_loc": {"city_name ":" Mumbai "," rue ":" galino1 "}} '. Je veux consommer ces données par jersey + moxy basé ressource ressource post sous forme de représentation pojo. S'il vous plaît suggérer.S'il vous plaît noter également que la représentation de cas de chameau normale de JSON fonctionne en utilisant l'extrait de code suivant donné mais j'ai l'exigence d'utiliser un cas de serpent.Service de repos basé sur Jersey Moxy ne consommant pas de charge utile de requête json postée imbriquée

@XmlRootElement 
public class Person { 

private String firstName; 
private String lastName; 

private AddressLoc addressLoc = new AddressLoc(); 

public Person(){}; 

public Address getAddressLoc() { 
    return addressLoc ; 
} 
public void setAddressLoc(Address address) { 
    this.addressLoc = address; 
} 

public String getFirstName() { 
    return firstName; 
} 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

} 

enfants/classe imbriquée

public class AddressLoc { 

private String cityName; 
private String street; 

public AddressLoc(){}; 

public String getCityName() { 
    return cityName; 
} 
public void setCityName(String city) { 
    this.city = city; 
} 
public String getStreet() { 
    return street; 
} 
public void setStreet(String street) { 
    this.street = street; 
} 

} 

Catégorie de ressources

import javax.ws.rs.Consumes; 
    import javax.ws.rs.POST; 
    import javax.ws.rs.Path; 
    import javax.ws.rs.Produces; 
    import javax.ws.rs.core.MediaType; 
    import com.nabisoft.tutorials.jerseymoxy.model.Person; 

@Path("/person") 
public class PersonResource { 

@POST 
@Consumes({MediaType.APPLICATION_JSON}) 
@Produces({MediaType.TEXT_PLAIN}) 
@Path("/post") 
public String postPerson(Person pers) throws Exception{ 

    System.out.println("First Name = "+pers.getFirstName()); 
    System.out.println("Last Name = "+pers.getLastName()); 
    System.out.println("Last Name = "+pers.getAddress().getCity()); 
    System.out.println("Last Name = "+pers.getAddress().getStreet()); 

    return "ok"; 
} 
} 

code client

<script src="<%=request.getContextPath() %>/js/jquery-1.11.2.min.js"></script> 
    <script> 
     var ctxPath = "<%=request.getContextPath() %>"; 
     $(function(){     
      $("#postPerson, #postMessage").on("click", function(){ 
       $.ajax({ 
        url: $(this).attr("id") === "postMessage" ? ctxPath+"/service/message/post" : ctxPath+"/service/personwa/post", 
        type: "POST", 
        data: '{"first_name":"Michael", "last_name":"Jordan", "address_loc":{"city_name":"Mumbai", "street":"galino1"}}', 
        contentType: "application/json", 
        cache: false, 
        dataType: "json" 
       }); 
      });     
     }); 
    </script> 

Outre ci-dessus énumérés codebase j'ai aussi "ApplicationConfig.java" et "JsonMoxyConfigurationCon textResolver.java "sans aucune implémentation fantaisie dedans. Merci.

+0

Quelle est la question? – Stefan

Répondre

2

Vous souhaitez ajouter javax.xml.bind.annotation.XmlElement et fournir un autre nom (cas de serpent).

@XmlRootElement 
public class Person { 

    @XmlElement(name="first_name") 
    private String firstName;