0

Ich versuche, eine Anforderung zu Daten Stax-Cassandra zu speichern. Komponenten: EventController: @RestEndPoint OneKeyClass: Für komplexen Schlüssel (mit mehreren Spalten) Ich verwende das Standard-Spring-Repository-Modell zum Speichern und Abrufen der Daten.Spring-Daten Cassandra, das JSON nicht abbildet Anfordern, wenn Schlüssel von POJO zusammengesetzt ist

Gradle: 
plugins { 
    id "org.springframework.boot" version "1.5.3.RELEASE" 
} 

apply plugin: 'java' 

jar { 
    baseName = 'sample-boot-with-cassandra' 
    version = '0.0.1-SNAPSHOT' 
} 
sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    mavenCentral() 
} 


dependencies { 
    compile('org.springframework.boot:spring-boot-starter-data-cassandra') 
    compile('org.springframework.boot:spring-boot-starter-web') 
    compile('org.springframework.kafka:spring-kafka') 
     compile('org.springframework.kafka:spring-kafka-test') 


    testCompile('org.springframework.boot:spring-boot-starter-test') 
} 

Controller:

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.PostMapping; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 

import com.company.employee.model.Event; 
import com.company.employee.service.EventService; 

@RestController 
@RequestMapping("/event") 
public class EventController { 

    @Autowired 
    private EventService eventService; 

    @PostMapping 
    public ResponseEntity<Event> saveEvent(@RequestBody Event event){ 
     return new ResponseEntity<Event>(eventService.saveEvent(event), HttpStatus.CREATED); 
    } 
} 

EventServiceImpl

package com.company.employee.service; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import com.company.employee.model.Event; 
import com.company.employee.model.LoginEvent; 
import com.company.employee.repository.EventRepository; 
import com.company.employee.repository.LoginEventRepository; 

@Service 
public class EventServiceImpl implements EventService { 
    private EventRepository eventRepository; 

    private LoginEventRepository loginEventRepository; 
    private Logger logger=LoggerFactory.getLogger(EmployeeServiceImpl.class); 

    @Autowired 
    public EventServiceImpl(EventRepository eventRepository,LoginEventRepository loginEventRepository) { 
     this.eventRepository=eventRepository; 
     this.loginEventRepository=loginEventRepository; 
    } 

    @Override 
    public Event saveEvent(Event event) { 
     logger.info("saving event"+event.toString()); 
     return eventRepository.save(event); 
    } 

    @Override 
    public LoginEvent saveEvent(LoginEvent event) { 
     return loginEventRepository.save(event); 
    } 

} 

Repository:

package com.company.employee.repository; 

import org.springframework.data.cassandra.repository.CassandraRepository; 

import com.company.employee.model.Event; 

public interface EventRepository extends CassandraRepository<Event> { 

} 

Ereignis:

package com.company.employee.model; 

import org.springframework.data.cassandra.mapping.Column; 
import org.springframework.data.cassandra.mapping.PrimaryKey; 
import org.springframework.data.cassandra.mapping.Table; 

@Table(value="event") 
public class Event { 

    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("Event [eventKey=").append(eventKey).append(", transactionstatus=").append(transactionstatus) 
       .append("]"); 
     return builder.toString(); 
    } 

    @PrimaryKey 
    private EventKey eventKey; 

    public EventKey getEventKey() { 
     return eventKey; 
    } 

    public void setEventKey(EventKey eventKey) { 
     this.eventKey = eventKey; 
    } 

    public String getTransactionstatus() { 
     return transactionstatus; 
    } 

    public void setTransactionstatus(String transactionstatus) { 
     this.transactionstatus = transactionstatus; 
    } 

    @Column(value="transactionstatus") 
    private String transactionstatus; 
} 

EventKey Paket com.company.employee.model;

import java.io.Serializable; 

import org.springframework.cassandra.core.Ordering; 
import org.springframework.cassandra.core.PrimaryKeyType; 
import org.springframework.data.cassandra.mapping.PrimaryKeyClass; 
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn; 

@PrimaryKeyClass 
public class EventKey implements Serializable { 

    @Override 
    public String toString() { 
     StringBuilder builder = new StringBuilder(); 
     builder.append("EventKey [eventsource=").append(eventsource).append(", eventid=").append(eventid) 
       .append(", eventstate=").append(eventstate).append("]"); 
     return builder.toString(); 
    } 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + eventid; 
     result = prime * result + ((eventsource == null) ? 0 : eventsource.hashCode()); 
     result = prime * result + ((eventstate == null) ? 0 : eventstate.hashCode()); 
     return result; 
    } 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     EventKey other = (EventKey) obj; 
     if (eventid != other.eventid) 
      return false; 
     if (eventsource == null) { 
      if (other.eventsource != null) 
       return false; 
     } else if (!eventsource.equals(other.eventsource)) 
      return false; 
     if (eventstate == null) { 
      if (other.eventstate != null) 
       return false; 
     } else if (!eventstate.equals(other.eventstate)) 
      return false; 
     return true; 
    } 
    public String getEventsource() { 
     return eventsource; 
    } 
    public void setEventsource(String eventsource) { 
     this.eventsource = eventsource; 
    } 
    public EventKey(String eventsource, int eventid, String eventstate) { 
     super(); 
     this.eventsource = eventsource; 
     this.eventid = eventid; 
     this.eventstate = eventstate; 
    } 
    public int getEventid() { 
     return eventid; 
    } 
    public void setEventid(int eventid) { 
     this.eventid = eventid; 
    } 
    public String getEventstate() { 
     return eventstate; 
    } 
    public void setEventstate(String eventstate) { 
     this.eventstate = eventstate; 
    } 
    @PrimaryKeyColumn(name="eventsource",ordinal=0,type=PrimaryKeyType.PARTITIONED) 
    private String eventsource; 

    @PrimaryKeyColumn(name="eventid",ordinal=1,type=PrimaryKeyType.CLUSTERED,ordering=Ordering.ASCENDING) 
    private int eventid; 

    @PrimaryKeyColumn(name="eventstate",ordinal=2,type=PrimaryKeyType.CLUSTERED,ordering=Ordering.ASCENDING) 
    private String eventstate; 


} 

Fehler: java.lang.IllegalArgumentException: Ziel Bohne nicht null sein müssen!

eventEvent Speichern [eventKey = null, Transaction = Erfolg]

Incoming Nutzlast:

{ 
    "eventsource" : "terminal", 
    "eventid": "23232", 
    "eventstate" : "CI", 
    "transactionstatus" : "success" 
} 

Ich folgte https://docs.spring.io/spring-data/cassandra/docs/1.0.2.RELEASE/reference/html/cassandra.core.html

Vielen Dank im Voraus für die Suche und versuchen zu helfen .

+0

Bitte fügen Sie die vollständige Stack-Trace ein. – mp911de

+0

Bitte finden Sie Pastebin https://pastebin.com/wBzZLhMb – MrWayne

Antwort

1

Die JSON-Nutzlast stimmt nicht mit Ihrer Objektstruktur überein. Ihre JSON Darstellung sollte eher entsprechen:

{ 
    "eventKey": { 
    "eventsource" : "terminal", 
    "eventid": "23232", 
    "eventstate" : "CI" 
    }, 
    "transactionstatus" : "success" 
} 

Alternativ Sie EventKey in Event oder zwei Strukturen unterschiedliche Daten verwenden Inline könnte, eine für die API das andere zu speichern und abfragen, Ihre Daten in Cassandra. IMHO Aufteilung Verantwortlichkeiten ist die bessere Option, Dinge zu entkoppeln, obwohl die Verwendung derselben Typen, um Ihre Daten durch Ihre API zu repräsentieren und in seiner dauerhaften Form erfordert weniger Code und keine Zuordnung.

+0

Ja, das ist wahr. Ich gab falsche json.Also beide POJO brauchen Standard-Konstruktoren sonst jackosn nicht in der Lage, die JSON zu Klasse zuordnen – MrWayne

Verwandte Themen