Posting JSON towards tomcat running Spring BOOT.
Mapping requests to update Mongo via Morphia
Getting Error : "status":500,"error":"Internal Server Error","exception":"org.bson.codecs.configuration.CodecConfigurationException","message":"Can't find a codec for class se.preffo.model.ProfileAccess.
Somehow it is not knowing how to create the Object ProfileAccess
ProfileClass
package se.preffo.model;
import java.util.List;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.*;
#Entity("profiles")
public class Profile extends BaseEntity {
#Property("user_name")
private String userName;
#Property("profile_data")
private List<ProfileData> profileData;
#Property("tags")
private List<String> profileTags;
#Property("profile_name")
private String profileName;
#Embedded
#Property("access")
private List<ProfileAccess> profileAccess;
public Profile (){
this.userName = "";
}
public String getUserName(){
return this.userName;
}
public void setUserName(String userId){
this.userName = userId;
}
public List<ProfileData> getProfileData(){
return this.profileData;
}
public void setProfileData(List<ProfileData> profileData){
this.profileData = profileData;
}
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}
public List<ProfileAccess> getProfileAccess() {
return profileAccess;
}
public void setProfileAccess(List<ProfileAccess> profileAccess) {
this.profileAccess = profileAccess;
}
public List<String> getProfileTags() {
return profileTags;
}
public void setProfileTags(List<String> profileTags) {
this.profileTags = profileTags;
}
public void addTag(String tag){
this.profileTags.add(tag);
}
public void removeTag(String tag){
this.profileTags.remove(tag);
}
#Override
public String toString() {
return "Profile{" +
"id=" + id +
", userName='" + userName + '\'' +
", profileData=" + profileData +
", profileTags=" + profileTags +
", profileName='" + profileName + '\'' +
", profileAccess=" + profileAccess +
'}';
}
}
ProfileAccess class:
package se.preffo.model;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Property;
#Embedded
public class ProfileAccess{
#Property("name")
private String accessName;
#Property("access_id")
private String accessId;
#Property("exp")
private String expiryTime;
#Property("type")
private String accessType;
//Constructor
public ProfileAccess() {
super();
}
#Override
public String toString() {
return "ProfileAccess{" +
"accessName='" + accessName + '\'' +
", accessId='" + accessId + '\'' +
", expiryTime='" + expiryTime + '\'' +
", accessType='" + accessType + '\'' +
'}';
}
public String getAccessName() {
return accessName;
}
public void setAccessName(String accessName) {
this.accessName = accessName;
}
public String getAccessId() {
return accessId;
}
public void setAccessId(String accessId) {
this.accessId = accessId;
}
public String getExpiryTime() {
return expiryTime;
}
public void setExpiryTime(String expiryTime) {
this.expiryTime = expiryTime;
}
public String getAccessType() {
return accessType;
}
public void setAccessType(String accessType) {
this.accessType = accessType;
}
}
ProfileController
// ---------------------- UPDATE LIST OF PROFILES
#RequestMapping(value="/profiles/batch", method=RequestMethod.POST)
public void updateProfile(#RequestBody List<Profile> profiles) {
logger.debug(profiles.get(0).toString());
profileService.updateProfiles(profiles);
}
ProfileService
public void updateProfiles(List<Profile> profiles) {
datastore = MongoDbHelper.INSTANCE.getDatastore();
for (Profile profile : profiles) {
logger.debug(profile.toString());
datastore.save(profile);
}
}
MongoDbHelper
private MongoDbHelper() {
MongoClient mongoClient = new MongoClient(new MongoClientURI("uritomongodb"));
Morphia morphia = new Morphia();
this.datastore = morphia.createDatastore(mongoClient, DATABASE_NAME);
}
public Datastore getDatastore() {
return this.datastore;
}
Posted JSON
[{"id":{"timestamp":1489743145,"machineIdentifier":13056160,"processIdentifier":3851,"counter":6420585,"time":1489743145000,"date":1489743145000,"timeSecond":1489743145},"version":14,"userName":"test#gmail.com","profileData":null,"profileTags":null,"profileName":"jonas","profileAccess":[{"accessId":"testare","expiryTime":"20170319","accessName":"testare","accessType":"shop"}]}]
Related
I have implemented Debezium MySql connector with Apache Flink. I am able to receive CDC event through Kafka. Insert and update event are working fine.But as soon as delete event is received, Flink job and application stops working with Job execution failed exception error. Delete event is received in my code and sink invoke method is getting triggered and my delete query is running successfully but after completion Flink job is stopped with error message Job execution failed. Not aware if it is a bug in Flink or any configuration is not right. I have attached my Flink main configuration class and Sink class.
**//Apache Flink job configuration and implementation class**
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
try{
Properties props = new Properties();
props.put("bootstrap.servers", "172.17.0.3:9092");
props.put("zookeeper.connect", "172.17.0.2:2181");
props.put("group.id", "metric-group");
// props.put("value.serializer", AppConstant.VALUE_SERIALIZER);
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("auto.offset.reset", "latest");
DataStreamSource<String> dataStreamSource = env.addSource(new FlinkKafkaConsumer<>(
"dbserver1.inventory.customers",
new SimpleStringSchema(),
props));
DataStream<MySqlCDCResponse> testEntity = dataStreamSource
.map(string -> new Gson().fromJson(string, MySqlCDCResponse.class));
testEntity.addSink(new MySqlSink());
System.out.println("Data::");
env.execute("MySqlApp");
}
catch (Exception ex){
System.out.println("Error -> " + ex.getMessage());
}
**//MySql Sink class that I am using with Apache Flink **
package entity;
import dto.MySqlCDCResponse;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Objects;
public class MySqlSink extends RichSinkFunction<MySqlCDCResponse> {
private PreparedStatement ps;
private Connection connection;
// private static final Logger log = LoggerFactory.getLogger(MySqlSink.class);
private static String DELETE_OPERATION = "select * from customers where email=?";
#Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
connection = getConnection();
String sql = "insert into customers (first_name, last_name, email) values( ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE first_name = VALUES(first_name) , last_name = VALUES(last_name);";
if (connection != null) {
ps = this.connection.prepareStatement(sql);
}
}
#Override
public void close() throws Exception {
super.close();
if (connection != null) {
connection.close();
}
if (ps != null) {
ps.close();
}
}
#Override
public void invoke(MySqlCDCResponse value, Context context) throws Exception {
System.out.println("Thread name:: " + Thread.currentThread().getName());
System.out.println("value= " + value.getPayload().toString());
if (ps == null) {
return;
}
try {
if(Objects.nonNull(value.getPayload().getBefore()) && Objects.isNull(value.getPayload().getAfter())) {
//connection = getConnection();
System.out.println("Delete Operation " + value.getPayload().toString());
ps = this.connection.prepareStatement(DELETE_OPERATION);
ps.setString(1, value.getPayload().getBefore().getEmail());
ps.executeQuery();
}
else {
ps.setString(1, value.getPayload().getAfter().getFirst_name());
ps.setString(2, value.getPayload().getAfter().getLast_name());
ps.setString(3, value.getPayload().getAfter().getEmail());
ps.executeUpdate();
}
}
catch (Exception ex) {
System.out.println("Error:: " + ex.getMessage());
}
}
private static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root#123");
} catch (Exception ex) {
// log.error("-----------mysql get connection has exception , msg = {}", e.getMessage());
System.out.println("Connection failure::" + ex.getMessage());
}
return con;
}
}
public class MySqlCDCResponse {
private int id;
private String first_name;
private String last_name;
private String email;
public MySqlCDCResponse(String first_name, String last_name, String email){
this.first_name =first_name;
this.last_name =last_name;
this.email =email;
}
private PayloadBean payload;
public PayloadBean getPayload() {
return payload;
}
public void setPayload(PayloadBean payload) {
this.payload = payload;
}
public static class PayloadBean {
private BeforeBean before;
private AfterBean after;
private SourceBean source;
private String op;
private long ts_ms;
private Object transaction;
public BeforeBean getBefore() {
return before;
}
public void setBefore(BeforeBean before) {
this.before = before;
}
public AfterBean getAfter() {
return after;
}
public void setAfter(AfterBean after) {
this.after = after;
}
public SourceBean getSource() {
return source;
}
public void setSource(SourceBean source) {
this.source = source;
}
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
public long getTs_ms() {
return ts_ms;
}
public void setTs_ms(long ts_ms) {
this.ts_ms = ts_ms;
}
public Object getTransaction() {
return transaction;
}
public void setTransaction(Object transaction) {
this.transaction = transaction;
}
public static class BeforeBean {
private int id;
private String first_name;
private String last_name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return "BeforeBean{" +
"id=" + id +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", email='" + email + '\'' +
'}';
}
}
public static class AfterBean {
private int id;
private String first_name;
private String last_name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return "AfterBean{" +
"id=" + id +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", email='" + email + '\'' +
'}';
}
}
public static class SourceBean {
private String version;
private String connector;
private String name;
private long ts_ms;
private String snapshot;
private String db;
private Object sequence;
private String table;
private int server_id;
private Object gtid;
private String file;
private int pos;
private int row;
private Object thread;
private Object query;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getConnector() {
return connector;
}
public void setConnector(String connector) {
this.connector = connector;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getTs_ms() {
return ts_ms;
}
public void setTs_ms(long ts_ms) {
this.ts_ms = ts_ms;
}
public String getSnapshot() {
return snapshot;
}
public void setSnapshot(String snapshot) {
this.snapshot = snapshot;
}
public String getDb() {
return db;
}
public void setDb(String db) {
this.db = db;
}
public Object getSequence() {
return sequence;
}
public void setSequence(Object sequence) {
this.sequence = sequence;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public int getServer_id() {
return server_id;
}
public void setServer_id(int server_id) {
this.server_id = server_id;
}
public Object getGtid() {
return gtid;
}
public void setGtid(Object gtid) {
this.gtid = gtid;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public int getPos() {
return pos;
}
public void setPos(int pos) {
this.pos = pos;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public Object getThread() {
return thread;
}
public void setThread(Object thread) {
this.thread = thread;
}
public Object getQuery() {
return query;
}
public void setQuery(Object query) {
this.query = query;
}
#Override
public String toString() {
return "SourceBean{" +
"version='" + version + '\'' +
", connector='" + connector + '\'' +
", name='" + name + '\'' +
", ts_ms=" + ts_ms +
", snapshot='" + snapshot + '\'' +
", db='" + db + '\'' +
", sequence=" + sequence +
", table='" + table + '\'' +
", server_id=" + server_id +
", gtid=" + gtid +
", file='" + file + '\'' +
", pos=" + pos +
", row=" + row +
", thread=" + thread +
", query=" + query +
'}';
}
}
#Override
public String toString() {
return "PayloadBean{" +
"before=" + before +
", after=" + after +
", source=" + source +
", op='" + op + '\'' +
", ts_ms=" + ts_ms +
", transaction=" + transaction +
'}';
}
}
#Override
public String toString() {
return "MySqlCDCResponse{" +
"payload=" + payload +
'}';
}
}
In my Spring Controller I have:
List<User> Users = this.userService.UsersList();
mav = new ModelAndView("users");
mav.addObject("Users", Users);
When I iterate over Users I can see all the attributes values of every element of my list.
This is my .jsp code:
<c:forEach items="${Users}" var="usr">
${usr}
</c:forEach>
This is my users class:
#Entity
#Table(name="usuario")
public class User implements Serializable {
#Id
#Column(name="id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
#JoinColumn(name = "id_perfil")
#OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
private Perfil perfil;
#Column(name="nombre")
private String nombre;
#Column(name="usuario")
private String usuario;
#Column(name="contrasenia")
private String contrasenia;
#Column(name="correo")
private String correo;
#Column(name="telefono")
private String telefono;
#Column(name="imagen_perfil")
private String imagen_perfil;
#Column(name="intento_fallido")
private int intento_fallido;
#Column(name="intranet_id")
private Integer intranet_id;
#Column(name="intranet_notaria")
private Integer intranet_notaria;
#Column(name="intranet_token_codigo")
private String intranet_token_codigo;
#Column(name="intranet_token_fecha")
#Temporal(TemporalType.TIMESTAMP)
private Date intranet_token_fecha;
#Column(name="tesoreria_token_codigo")
private String tesoreria_token_codigo;
#Column(name="tesoreria_token_fecha")
#Temporal(TemporalType.TIMESTAMP)
private Date tesoreria_token_fecha;
#Column(name="activo")
private int activo;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public Perfil getPerfil() { return perfil; }
public void setPerfil(Perfil perfil) { this.perfil = perfil; }
public String getNombre() { return nombre; }
public void setNombre(String nombre) { this.nombre = nombre; }
public String getUsuario() { return usuario; }
public void setUsuario(String usuario) { this.usuario = usuario; }
public String getContrasenia() { return contrasenia; }
public void setContrasenia(String contrasenia) { this.contrasenia = contrasenia; }
public String getCorreo() { return correo; }
public void setCorreo(String correo) { this.correo = correo; }
public String getTelefono() { return telefono; }
public void setTelefono(String telefono) { this.telefono = telefono; }
public String getImagenPerfil() { return imagen_perfil; }
public void setImagenPerfil(String imagen_perfil) { this.imagen_perfil = imagen_perfil; }
public int getIntentoFallido() { return intento_fallido; }
public void setIntentoFallido(int intento_fallido) { this.intento_fallido = intento_fallido; }
public Integer getIntranetId() { return intranet_id; }
public void setIntranetId(Integer intranet_id) { this.intranet_id = intranet_id; }
public Integer getIntranetNotaria() { return intranet_notaria; }
public void setIntranetNotaria(Integer intranet_notaria) { this.intranet_notaria = intranet_notaria; }
public String getIntranetTokenCodigo() { return intranet_token_codigo; }
public void setIntranetTokenCodigo(String intranet_token_codigo) { this.intranet_token_codigo = intranet_token_codigo; }
public Date getIntranetTokenFecha() { return intranet_token_fecha; }
public void setIntranetTokenFecha(Date intranet_token_fecha) { this.intranet_token_fecha = intranet_token_fecha; }
public String getTesoreriaTokenCodigo() { return tesoreria_token_codigo; }
public void setTesoreriaTokenCodigo(String tesoreria_token_codigo) { this.tesoreria_token_codigo = tesoreria_token_codigo; }
public Date getTesoreriaTokenFecha() { return tesoreria_token_fecha; }
public void setTesoreriaTokenFecha(Date tesoreria_token_fecha) { this.tesoreria_token_fecha = tesoreria_token_fecha; }
public int getActivo() { return activo; }
public void setActivo(int activo) { this.activo = activo; }
#Override
public String toString() {
return "Id:" + id + ", " + "Perfil:" + perfil.getNombre() + ", " + "Id_Perfil:" + perfil.getId() + ", " + "Nombre:" + nombre + ", " + "Usuario:" + usuario + ", " + "Correo:" + correo + ", " + "Teléfono:" + telefono + ", " + "Image_Perfil:" + imagen_perfil + ", " + "Intranet_Id:" + intranet_id + ", " + "Intranet_Notaria:" + intranet_notaria + ", " + "Activo:" + activo;
}
}
The problem is that ${usr} is only displaying some of my attributes, but not all! I need to display all the attributes in my jsp.
Try using camelCase notation if you are not doing so. For example instead of ${usr.imagen_perfil} use ${usr.imagenPerfil}. I think it will be expecting to use the object getters getImagenPerfil().
i want filter my task depend the work id
but when run my code i have this error
java.util.stream.ReferencePipeline$3 cannot be cast to org.springframework.data.domain.Page
how i can solve these
this is my component
my services here:
public Page<TaskDataDTO> findAll(String workOrderId,Pageable pageable) {
log.debug("Request to get all TaskData");
if(workOrderId!=null)
return (Page<TaskDataDTO>) taskDataRepository.findAll(pageable).stream().filter(x-> x.getWorkOrderId()==workOrderId).map(taskDataMapper::toDto);
else
return null;
}
and my Controller here :
#GetMapping("/task-data/{workOrderId}")
public ResponseEntity<List<TaskDataDTO>> getAllTaskData(#PathVariable String workOrderId,Pageable pageable) {
if(workOrderId!=null)
{
log.debug("REST request to get a page of TaskData");
Page<TaskDataDTO> page = taskDataService.findAll(workOrderId,pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
else {
return null;
}
}
the task entity :
package com.asc.skyalign.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import java.io.Serializable;
/**
* A TaskData.
*/
#Document(collection = "task_data")
public class TaskData implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private String id;
#Field("roll")
private String roll;
#Field("azimuth")
private String azimuth;
#Field("tilt")
private String tilt;
#Field("sector_location_lat")
private String sectorLocationLat;
#Field("amt_imei")
private String amtImei;
#Field("wakeu_up_interval")
private String wakeuUpInterval;
#Field("sector_id")
private String sectorID;
#Field("sector_location_long")
private String sectorLocationLong;
#Field("task_name")
private String taskName;
#Field("task_description")
private String taskDescription;
#Field("work_order_id")
private String workOrderId;
private WorkOrder workOrder;
public void setWorkOrder(WorkOrder workOrder) {
this.workOrder = workOrder;
}
public WorkOrder getWorkOrder() {
return workOrder;
}
// jhipster-needle-entity-add-field - JHipster will add fields here
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRoll() {
return roll;
}
public TaskData roll(String roll) {
this.roll = roll;
return this;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getAzimuth() {
return azimuth;
}
public TaskData azimuth(String azimuth) {
this.azimuth = azimuth;
return this;
}
public void setAzimuth(String azimuth) {
this.azimuth = azimuth;
}
public String getTilt() {
return tilt;
}
public TaskData tilt(String tilt) {
this.tilt = tilt;
return this;
}
public void setTilt(String tilt) {
this.tilt = tilt;
}
public String getSectorLocationLat() {
return sectorLocationLat;
}
public TaskData sectorLocationLat(String sectorLocationLat) {
this.sectorLocationLat = sectorLocationLat;
return this;
}
public void setSectorLocationLat(String sectorLocationLat) {
this.sectorLocationLat = sectorLocationLat;
}
public String getAmtImei() {
return amtImei;
}
public TaskData amtImei(String amtImei) {
this.amtImei = amtImei;
return this;
}
public void setAmtImei(String amtImei) {
this.amtImei = amtImei;
}
public String getWakeuUpInterval() {
return wakeuUpInterval;
}
public TaskData wakeuUpInterval(String wakeuUpInterval) {
this.wakeuUpInterval = wakeuUpInterval;
return this;
}
public void setWakeuUpInterval(String wakeuUpInterval) {
this.wakeuUpInterval = wakeuUpInterval;
}
public String getSectorID() {
return sectorID;
}
public TaskData sectorID(String sectorID) {
this.sectorID = sectorID;
return this;
}
public void setSectorID(String sectorID) {
this.sectorID = sectorID;
}
public String getSectorLocationLong() {
return sectorLocationLong;
}
public TaskData sectorLocationLong(String sectorLocationLong) {
this.sectorLocationLong = sectorLocationLong;
return this;
}
public void setSectorLocationLong(String sectorLocationLong) {
this.sectorLocationLong = sectorLocationLong;
}
public String getTaskName() {
return taskName;
}
public TaskData taskName(String taskName) {
this.taskName = taskName;
return this;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getTaskDescription() {
return taskDescription;
}
public TaskData taskDescription(String taskDescription) {
this.taskDescription = taskDescription;
return this;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
public String getWorkOrderId() {
return workOrderId;
}
public TaskData workOrderId(String workOrderId) {
this.workOrderId = workOrderId;
return this;
}
public void setWorkOrderId(String workOrderId) {
this.workOrderId = workOrderId;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TaskData)) {
return false;
}
return id != null && id.equals(((TaskData) o).id);
}
#Override
public int hashCode() {
return 31;
}
// prettier-ignore
#Override
public String toString() {
return "TaskData{" +
"id=" + getId() +
", roll='" + getRoll() + "'" +
", azimuth='" + getAzimuth() + "'" +
", tilt='" + getTilt() + "'" +
", sectorLocationLat='" + getSectorLocationLat() + "'" +
", amtImei='" + getAmtImei() + "'" +
", wakeuUpInterval='" + getWakeuUpInterval() + "'" +
", sectorID='" + getSectorID() + "'" +
", sectorLocationLong='" + getSectorLocationLong() + "'" +
", taskName='" + getTaskName() + "'" +
", taskDescription='" + getTaskDescription() + "'" +
", workOrderId='" + getWorkOrderId() + "'" +
"}";
}
}
and the TaskDto is here:
package com.asc.skyalign.service.dto;
import java.io.Serializable;
/**
* A DTO for the {#link com.asc.skyalign.domain.TaskData} entity.
*/
public class TaskDataDTO implements Serializable {
private String id;
private String roll;
private String azimuth;
private String tilt;
private String sectorLocationLat;
private String amtImei;
private String wakeuUpInterval;
private String sectorID;
private String sectorLocationLong;
private String taskName;
private String taskDescription;
private String workOrderId;
private WorkOrderDTO workOrderDTO;
public void setWorkOrderDTO(WorkOrderDTO workOrderDTO) {
this.workOrderDTO = workOrderDTO;
}
public WorkOrderDTO getWorkOrderDTO() {
return workOrderDTO;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getAzimuth() {
return azimuth;
}
public void setAzimuth(String azimuth) {
this.azimuth = azimuth;
}
public String getTilt() {
return tilt;
}
public void setTilt(String tilt) {
this.tilt = tilt;
}
public String getSectorLocationLat() {
return sectorLocationLat;
}
public void setSectorLocationLat(String sectorLocationLat) {
this.sectorLocationLat = sectorLocationLat;
}
public String getAmtImei() {
return amtImei;
}
public void setAmtImei(String amtImei) {
this.amtImei = amtImei;
}
public String getWakeuUpInterval() {
return wakeuUpInterval;
}
public void setWakeuUpInterval(String wakeuUpInterval) {
this.wakeuUpInterval = wakeuUpInterval;
}
public String getSectorID() {
return sectorID;
}
public void setSectorID(String sectorID) {
this.sectorID = sectorID;
}
public String getSectorLocationLong() {
return sectorLocationLong;
}
public void setSectorLocationLong(String sectorLocationLong) {
this.sectorLocationLong = sectorLocationLong;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
public String getWorkOrderId() {
return workOrderId;
}
public void setWorkOrderId(String workOrderId) {
this.workOrderId = workOrderId;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TaskDataDTO)) {
return false;
}
return id != null && id.equals(((TaskDataDTO) o).id);
}
#Override
public int hashCode() {
return 31;
}
// prettier-ignore
#Override
public String toString() {
return "TaskDataDTO{" +
"id=" + getId() +
", roll='" + getRoll() + "'" +
", azimuth='" + getAzimuth() + "'" +
", tilt='" + getTilt() + "'" +
", sectorLocationLat='" + getSectorLocationLat() + "'" +
", amtImei='" + getAmtImei() + "'" +
", wakeuUpInterval='" + getWakeuUpInterval() + "'" +
", sectorID='" + getSectorID() + "'" +
", sectorLocationLong='" + getSectorLocationLong() + "'" +
", taskName='" + getTaskName() + "'" +
", taskDescription='" + getTaskDescription() + "'" +
", workOrderId='" + getWorkOrderId() + "'" +
"}";
}
}
Thank you.
return (Page<TaskDataDTO>) taskDataRepository.findAll(pageable).stream().filter(x-> x.getWorkOrderId()==workOrderId).map(taskDataMapper::toDto);
The line above is missing a terminal operation to consume the stream. Try adding something like .collect(Collectors.toList())
Here's the package description for java.util.stream
I am trying to parse the JSON array and add it to my adapter class.
My code:
String url = "https://api.github.com/users";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray results = response.getJSONArray(); // error in this line since there is no array name
for(int i = 0;i < results.length();i++){
JSONObject result = results.getJSONObject(i);
String name = result.getString("login");
users.add(new User(name.substring(0,1).toUpperCase() + name.substring(1),result.getString("avatar_url")));
}
// notify that data has changed in recyclerview
notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
Log.e("cs50", "Json error", e);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("cs50","Github User List error",error);
}
});
You can see that I'm trying to get a response from Github API URL but I'm getting error as there is no array name but my code requires a parameter. how can I parse it ?
Change this line
StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://api.github.com/users", response -> {
Log.d(TAG, "_ApiGetGithubUsers: " + response);
if (response != null && !response.isEmpty()) {
try {
JSONArray usersArray = new JSONArray(response);
for (int i = 0; i < usersArray.length(); i++) {
JSONObject user = usersArray.getJSONObject(i);
Log.d(TAG, "_ApiGetGithubUsers: "+user.getString("login"));
}
} catch (Exception e) {
Log.d(TAG, "_ApiGetGithubUsers: " + e);
Toast.makeText(context, R.string.someErrorOccurred, Toast.LENGTH_SHORT).show();
}
} else
Toast.makeText(context, R.string.someErrorOccurred, Toast.LENGTH_SHORT).show();
}, error -> {
});
AppController.getInstance().addToRequestQueue(stringRequest);
This is the method i used and it is working and parsing attaching the log image for reference.
I think you can use retrofit library, add the dependencies to app gradle.
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:retrofit:2.5'
Create User Class to get Response.
public class User {
#SerializedName("login")
#Expose
private String login;
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("node_id")
#Expose
private String nodeId;
#SerializedName("avatar_url")
#Expose
private String avatarUrl;
#SerializedName("gravatar_id")
#Expose
private String gravatarId;
#SerializedName("url")
#Expose
private String url;
#SerializedName("html_url")
#Expose
private String htmlUrl;
#SerializedName("followers_url")
#Expose
private String followersUrl;
#SerializedName("following_url")
#Expose
private String followingUrl;
#SerializedName("gists_url")
#Expose
private String gistsUrl;
#SerializedName("starred_url")
#Expose
private String starredUrl;
#SerializedName("subscriptions_url")
#Expose
private String subscriptionsUrl;
#SerializedName("organizations_url")
#Expose
private String organizationsUrl;
#SerializedName("repos_url")
#Expose
private String reposUrl;
#SerializedName("events_url")
#Expose
private String eventsUrl;
#SerializedName("received_events_url")
#Expose
private String receivedEventsUrl;
#SerializedName("type")
#Expose
private String type;
#SerializedName("site_admin")
#Expose
private Boolean siteAdmin;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNodeId() {
return nodeId;
}
public void setNodeId(String nodeId) {
this.nodeId = nodeId;
}
public String getAvatarUrl() {
return avatarUrl;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public String getGravatarId() {
return gravatarId;
}
public void setGravatarId(String gravatarId) {
this.gravatarId = gravatarId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getHtmlUrl() {
return htmlUrl;
}
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}
public String getFollowersUrl() {
return followersUrl;
}
public void setFollowersUrl(String followersUrl) {
this.followersUrl = followersUrl;
}
public String getFollowingUrl() {
return followingUrl;
}
public void setFollowingUrl(String followingUrl) {
this.followingUrl = followingUrl;
}
public String getGistsUrl() {
return gistsUrl;
}
public void setGistsUrl(String gistsUrl) {
this.gistsUrl = gistsUrl;
}
public String getStarredUrl() {
return starredUrl;
}
public void setStarredUrl(String starredUrl) {
this.starredUrl = starredUrl;
}
public String getSubscriptionsUrl() {
return subscriptionsUrl;
}
public void setSubscriptionsUrl(String subscriptionsUrl) {
this.subscriptionsUrl = subscriptionsUrl;
}
public String getOrganizationsUrl() {
return organizationsUrl;
}
public void setOrganizationsUrl(String organizationsUrl) {
this.organizationsUrl = organizationsUrl;
}
public String getReposUrl() {
return reposUrl;
}
public void setReposUrl(String reposUrl) {
this.reposUrl = reposUrl;
}
public String getEventsUrl() {
return eventsUrl;
}
public void setEventsUrl(String eventsUrl) {
this.eventsUrl = eventsUrl;
}
public String getReceivedEventsUrl() {
return receivedEventsUrl;
}
public void setReceivedEventsUrl(String receivedEventsUrl) {
this.receivedEventsUrl = receivedEventsUrl;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Boolean getSiteAdmin() {
return siteAdmin;
}
public void setSiteAdmin(Boolean siteAdmin) {
this.siteAdmin = siteAdmin;
}
#Override
public String toString() {
return "User{" +
"login='" + login + '\'' +
", id=" + id +
", nodeId='" + nodeId + '\'' +
", avatarUrl='" + avatarUrl + '\'' +
", gravatarId='" + gravatarId + '\'' +
", url='" + url + '\'' +
", htmlUrl='" + htmlUrl + '\'' +
", followersUrl='" + followersUrl + '\'' +
", followingUrl='" + followingUrl + '\'' +
", gistsUrl='" + gistsUrl + '\'' +
", starredUrl='" + starredUrl + '\'' +
", subscriptionsUrl='" + subscriptionsUrl + '\'' +
", organizationsUrl='" + organizationsUrl + '\'' +
", reposUrl='" + reposUrl + '\'' +
", eventsUrl='" + eventsUrl + '\'' +
", receivedEventsUrl='" + receivedEventsUrl + '\'' +
", type='" + type + '\'' +
", siteAdmin=" + siteAdmin +
'}';
}
}
Create a API interface for endpoint URL.
public interface APIInterface {
#GET("/users")
Call<List<User>> getGitHubUser();
}
Create Retrofit API Client.
public class APIClient {
/**
* Get Retrofit Instance
*/
private static Retrofit getRetrofitInstance() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
return new Retrofit.Builder()
.baseUrl("https://api.github.com")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
public static APIInterface getApiInterface() {
return getRetrofitInstance().create(APIInterface.class);
}
}
Then Call the below method to get your parse data.
public void getUserList() {
APIInterface apiInterface=APIClient.getApiInterface();
Call<List<User>> call= apiInterface.getGitHubUser();
call.enqueue(new Callback<List<User>>() {
#Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
List<User> userList= response.body();
System.out.println(userList);
}
#Override
public void onFailure(Call<List<User>> call, Throwable t) {
}
});
}
I am working on Springboot REST API. My application should consume and produce both XML and JSON. I came across the Jackson json Xml dependency.
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.5.4</version>
</dependency>
I added this in my pom.xml. Now I am able to accept xml input but the values are null when mapped to Java Object. The following is my Resource class.
#Configuration
#ImportResource("/application-context.xml")
#EnableAutoConfiguration
#ResponseBody
#RequestMapping("/match")
public class MatchResource {
private static final Logger logger = LogManager.getLogger(MatchResource.class);
#Autowired
private MatchService matchService;
#RequestMapping(method = RequestMethod.POST)
#Consumes({MediaType.TEXT_XML,MediaType.APPLICATION_JSON})
#Produces({MediaType.TEXT_XML,MediaType.APPLICATION_JSON})
//#Produces( MediaType.APPLICATION_XML)
public Response matchRequest(#RequestBody MatchRequest matchRequest,
#Context HttpServletRequest headers) throws Exception {
Response resp = null;
MiniMatchResponse output = null;
// Headers are store in the "headers" object. To retrieve a specific header, please take a look at the below statement
String apiUser = headers.getHeader("Api-User");
UUID randID = UUID.randomUUID();
logger.info("Get Match for with ID: " + randID);
// Get service profile from headers via MatchConstants.SERVICE_PROFILE_HEADER
String serviceProfile = "";
try {
//TODO: MatchService should return MatchResponse Object
//Json OutPut
output = matchService.findResponse(matchRequest, serviceProfile);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
logger.debug("Match Request: " + matchRequest.toString());
} catch (ErrorException e) {
logger.error(e.getMessage(), e);
}
// Form Response
resp = Response.status(200).entity(output).build();
return resp;
}
The below is my Request Object
package com.infoconnect.api.dto.Match;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.List;
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonIgnoreProperties(ignoreUnknown = true)
public class MatchRequest implements Serializable {
// Gets or sets the RequestType of request this represents.
// Allowed values are "Company", "People" and "Any".
private String requestType;
private String name;
private String companyName;
private String streetAddress;
private String streetAddress2;
private String city;
private String stateProvince;
private String postalCode;
private String country;
private String serviceProfile;
private String resourceType;
private int limit;
private Integer confidence;
private String phone;
private Boolean includeHistorical;
private Boolean includeNonVerified;
private String requestId;
private List<String> fields;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getStreetAddress2() {
return streetAddress2;
}
public void setStreetAddress2(String streetAddress2) {
this.streetAddress2 = streetAddress2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStateProvince() {
return stateProvince;
}
public void setStateProvince(String stateProvince) {
this.stateProvince = stateProvince;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getServiceProfile() {
return serviceProfile;
}
public void setServiceProfile(String serviceProfile) {
this.serviceProfile = serviceProfile;
}
public String getResourceType() {
return resourceType;
}
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public Integer getConfidence() {
return confidence;
}
public void setConfidence(Integer confidence) {
this.confidence = confidence;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Boolean getIncludeHistorical() {
return includeHistorical;
}
public void setIncludeHistorical(Boolean includeHistorical) {
this.includeHistorical = includeHistorical;
}
public Boolean getIncludeNonVerified() {
return includeNonVerified;
}
public void setIncludeNonVerified(Boolean includeNonVerified) {
this.includeNonVerified = includeNonVerified;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public List<String> getFields() {
return fields;
}
public void setFields(List<String> fields) {
this.fields = fields;
}
#Override
public String toString() {
return "MatchRequest{" +
"requestType='" + requestType + '\'' +
", name='" + name + '\'' +
", companyName='" + companyName + '\'' +
", streetAddress='" + streetAddress + '\'' +
", streetAddress2='" + streetAddress2 + '\'' +
", city='" + city + '\'' +
", stateProvince='" + stateProvince + '\'' +
", postalCode='" + postalCode + '\'' +
", country='" + country + '\'' +
", serviceProfile='" + serviceProfile + '\'' +
", resourceType='" + resourceType + '\'' +
", limit=" + limit +
", confidence=" + confidence +
", phone='" + phone + '\'' +
", includeHistorical=" + includeHistorical +
", includeNonVerified=" + includeNonVerified +
", requestId='" + requestId + '\'' +
", fields=" + fields +
'}';
}
}
JSON request and Response works fine. Can you please help me how to Include XML request and Response in my application.
Try to add a #XmlRootElement(name="myRootTag") JAXB annotation with the tag you use as the root tag to the class MatchRequest. I have had similar issues when using both XML and JSON as transport format in a REST request, but using moxy instead of Jackson. In any case, proper JAXB annotations are necessary to convert to/from XML (XML is much pickier in this respect than JSON).
Jackson XML is supposed to support JAXB annotations, and if this does not work, it has an own set of similar annotations that are incompatible to JAXB (see https://github.com/FasterXML/jackson-dataformat-xml and https://github.com/FasterXML/jackson-dataformat-xml/wiki/Jackson-XML-annotations)