0

Data Stax-Cassandra에 요청을 저장하려고합니다. 구성 요소 : EventController : @RestEndPoint OneKeyClass : 복합 키 (여러 열 포함) 표준 스프링 리포지토리 모델을 사용하여 데이터를 저장하고 가져옵니다.스프링 데이터 Cassandra 매핑 안 함 JSON POJO의 키가 복합 인 경우 요청

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') 
} 

컨트롤러 :

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); 
    } 

} 

저장소 :

package com.company.employee.repository; 

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

import com.company.employee.model.Event; 

public interface EventRepository extends CassandraRepository<Event> { 

} 

이벤트 :

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 패키지 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; 


} 

오류 : java.lang.IllegalArgumentException가 : 대상 콩이 null이 아니어야합니다!

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

내가보고 도와 주려고 미리 https://docs.spring.io/spring-data/cassandra/docs/1.0.2.RELEASE/reference/html/cassandra.core.html

감사합니다 다음 : eventEvent [eventKey = null이, 된 TransactionStatus = 성공]

수신 페이로드를 저장

.

+0

전체 스택 추적을 포함하십시오. – mp911de

+0

pastebin https://pastebin.com/wBzZLhMb – MrWayne

답변

1

JSON 페이로드가 개체 구조와 일치하지 않습니다. 귀하의 JSON 표현은 오히려 일치해야합니다 :

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

다른 방법으로, EventEventKey 인라인 또는 카산드라에 데이터를 저장하고 조회하는 두 개의 서로 다른 데이터 구조, API에 대한 하나의 다른 하나를 사용할 수 있습니다. IMHO 분할 책임은 API를 통해 데이터를 나타 내기 위해 동일한 유형을 사용하고 영구적 인 형태로 적은 코드와 매핑이 필요하지만 일을 분리 된 상태로 유지하는 더 좋은 옵션입니다.

+0

네가 틀림 없어. json을 잘못주고있다. 또한 두 POJO에 기본 constuctors가 필요하다. 그렇지 않으면 jackosn이 json을 클래스에 매핑 할 수 없다. – MrWayne