2012-06-13 13 views
10

Ho un requisito per passare un oggetto personalizzato utilizzando RESTTemplate al mio servizio REST.Come passare oggetti personalizzati utilizzando il modello REST di Spring

RestTemplate restTemplate = new RestTemplate(); 
MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<String, Object>(); 
... 

requestMap.add("file1", new FileSystemResource(..); 
requestMap.add("Content-Type","text/html"); 
requestMap.add("accept", "text/html"); 
requestMap.add("myobject",new CustomObject()); // This is not working 
System.out.println("Before Posting Request........"); 
restTemplate.postForLocation(url, requestMap);//Posting the data. 
System.out.println("Request has been executed........"); 

Non riesco ad aggiungere il mio oggetto personalizzato a MultiValueMap. La generazione delle richieste sta fallendo.

Qualcuno può aiutarmi a trovare un modo per questo? Posso semplicemente passare un oggetto stringa senza problemi. Gli oggetti definiti dall'utente fanno il problema.

Apprezzare qualsiasi aiuto !!!

risposta

26

You can do it fairly simply with Jackson.

Ecco cosa ho scritto per un post di un semplice POJO.

@XmlRootElement(name="newobject") 
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 
public class NewObject{ 
    private String stuff; 

    public String getStuff(){ 
     return this.stuff; 
    } 

    public void setStuff(String stuff){ 
     this.stuff = stuff; 
    } 
} 

.... 
//make the object 
NewObject obj = new NewObject(); 
obj.setStuff("stuff"); 

//set your headers 
HttpHeaders headers = new HttpHeaders(); 
headers.setContentType(MediaType.APPLICATION_JSON); 

//set your entity to send 
HttpEntity entity = new HttpEntity(obj,headers); 

// send it! 
ResponseEntity<String> out = restTemplate.exchange("url", HttpMethod.POST, entity 
    , String.class); 

Il link qui sopra dovrebbe dirvi come configurarlo, se necessario. È un buon tutorial.

+0

?? – KJEjava48

+1

@ KJEjava48 Per ricevere NewObject in RestController '@PostMapping ("/create ") public ResponseEntity createNewObject (@RequestBody NewObject newObject) {// crea le tue cose}' – Darshan

2

Per ricevere NewObject in RestController

@PostMapping("/create") public ResponseEntity<String> createNewObject(@RequestBody NewObject newObject) { // do your stuff} 
0

si può provare questo Come posso ricevere questo NewObject nel server (vale a dire, il ricevitore) fine

public int insertParametro(Parametros parametro) throws LlamadasWSBOException { 
     String metodo = "insertParam"; 
     String URL_WS = URL_WS_BASE + metodo; 

     Integer request = null; 

     try { 
      logger.info("URL_WS: " + URL_WS); 

      request = restTemplate.postForObject(URL_WS, parametro, Integer.class); 

     } catch (RestClientResponseException rre) { 
      logger.error("RestClientResponseException insertParametro [WS BO]: " + rre.getResponseBodyAsString()); 
      logger.error("RestClientResponseException insertParametro [WS BO]: ", rre); 
      throw new CallWSBOException(rre.getResponseBodyAsString()); 
     } catch (Exception e) { 
      logger.error("Exception insertParametro[WS BO]: ", e); 
      throw new CallWSBOException(e.getMessage()); 
     } 
     return request; 
    } 
Problemi correlati