Changed the default spring serializer... It seems to have a bug

regarding List<Object>, plus an incompatibility with a Json annotation
This commit is contained in:
Nikolaos Laskaris 2017-10-24 19:33:51 +03:00
parent c5f21e566f
commit 4b211e8664
31 changed files with 257 additions and 236 deletions

View File

@ -4,7 +4,6 @@
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>
<property name="context-root" value="dmp-backend"/>
<property name="java-output-path" value="/dmp-backend/target/classes"/>
</wb-module>

View File

@ -143,10 +143,10 @@
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.8.4</version>
</dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.8.4</version>
</dependency>
@ -167,6 +167,7 @@
<scope>provided</scope>
</dependency>
<!-- FOR TESTING -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -174,6 +175,14 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<scope>test</scope>
</dependency>
<!-- PostGreSql base libs dependencies -->
<dependency>
<groupId>org.postgresql</groupId>

View File

@ -0,0 +1,37 @@
package configs;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Hibernate5Module module = new Hibernate5Module();
module.enable(Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
ObjectMapper objectMapper = new ObjectMapper()
// .setSerializationInclusion(Include.NON_NULL)
// .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new Hibernate5Module())
;
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
System.out.println("Using custom json serializer loaded with module Hibernate5Module");
}
}

View File

@ -35,9 +35,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
@Entity
@Table(name="\"DMP\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DMP implements Serializable {
@ -64,7 +62,7 @@ public class DMP implements Serializable {
private Set<Dataset> dataset;
@ManyToOne()
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "\"Project\"")
private Project project;
@ -73,7 +71,7 @@ public class DMP implements Serializable {
@Column(name = "\"ProfileData\"", columnDefinition = "xml", nullable = true)
private String profileData;
@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "\"Profile\"")
private DMPProfile profile;

View File

@ -22,9 +22,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DMPOrganisation\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DMPOrganisation implements Serializable {

View File

@ -2,14 +2,15 @@ package entities;
import java.io.Serializable;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@ -25,9 +26,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DMPProfile\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DMPProfile implements Serializable {
private static final long serialVersionUID = 3213099144442689808L;
@ -42,8 +41,8 @@ public class DMPProfile implements Serializable {
private UUID id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "profile")
private DMP dmp;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "profile")
private Set<DMP> dmps;
@Column(name = "\"Label\"")
@ -77,15 +76,13 @@ public class DMPProfile implements Serializable {
this.definition = definition;
}
public DMP getDmp() {
return dmp;
public Set<DMP> getDmps() {
return dmps;
}
public void setDmp(DMP dmp) {
this.dmp = dmp;
public void setDmps(Set<DMP> dmps) {
this.dmps = dmps;
}
}

View File

@ -22,9 +22,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DMPResearcher\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DMPResearcher implements Serializable {
private static final long serialVersionUID = 135449857355252959L;

View File

@ -26,9 +26,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DataRepository\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DataRepository implements Serializable {

View File

@ -30,14 +30,12 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity(name = "Dataset")
@Table(name="\"Dataset\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Dataset implements Serializable {
private static final long serialVersionUID = 3575723814399553259L;
public Dataset () {}
//public Dataset () {}
@Id
@ -50,7 +48,7 @@ public class Dataset implements Serializable {
private String label;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
// @Cascade(value=org.hibernate.annotations.CascadeType.ALL)
@JoinColumn(name = "\"DMP\"", nullable = true)
private DMP dmp;
@ -64,7 +62,7 @@ public class Dataset implements Serializable {
private String properties;
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
//@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
@JoinColumn(name = "\"Profile\"", nullable = true)
private DatasetProfile profile;

View File

@ -22,9 +22,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DatasetDataRepository\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DatasetDataRepository implements Serializable {

View File

@ -24,9 +24,7 @@ import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity
@Table(name="\"DatasetProfile\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DatasetProfile implements Serializable {

View File

@ -27,9 +27,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DatasetProfileRuleset\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DatasetProfileRuleset implements Serializable {
private static final long serialVersionUID = 4249565129050898613L;
@ -44,7 +42,7 @@ public class DatasetProfileRuleset implements Serializable {
private UUID id;
@OneToOne(fetch = FetchType.EAGER, mappedBy = "ruleset")
@OneToOne(fetch = FetchType.LAZY, mappedBy = "ruleset")
private DatasetProfile datasetProfile;

View File

@ -26,9 +26,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DatasetProfileViewstyle\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DatasetProfileViewstyle implements Serializable {
private static final long serialVersionUID = -3251295258160291468L;
@ -43,7 +41,7 @@ public class DatasetProfileViewstyle implements Serializable {
private UUID id;
@OneToOne(fetch = FetchType.EAGER, mappedBy = "viewstyle")
@OneToOne(fetch = FetchType.LAZY, mappedBy = "viewstyle")
private DatasetProfile datasetProfile;
@Column(name = "\"Label\"")

View File

@ -22,9 +22,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DatasetRegistry\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DatasetRegistry implements Serializable {
private static final long serialVersionUID = -5594183793725819187L;

View File

@ -22,9 +22,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"DatasetService\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class DatasetService implements Serializable {
private static final long serialVersionUID = 1469161193939096037L;

View File

@ -28,9 +28,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"Organisation\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Organisation implements Serializable {

View File

@ -32,8 +32,6 @@ import com.fasterxml.jackson.databind.SerializationFeature;
@Entity
@Table(name="\"Project\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Project implements Serializable {

View File

@ -28,9 +28,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"Registry\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Registry implements Serializable {
private static final long serialVersionUID = -277572262583178090L;

View File

@ -28,9 +28,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"Researcher\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Researcher implements Serializable {

View File

@ -30,14 +30,10 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"Service\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Service implements Serializable {
private static final long serialVersionUID = 7243446610500174060L;
public Service () {}
private static final long serialVersionUID = 163279108676904730L;
@Id
@ -65,7 +61,7 @@ public class Service implements Serializable {
private String definition;
@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name="\"DatasetService\"",
joinColumns={@JoinColumn(name="\"Service\"", referencedColumnName="\"ID\"")},
inverseJoinColumns={@JoinColumn(name="\"Dataset\"", referencedColumnName="\"ID\"")}

View File

@ -32,9 +32,7 @@ import entities.security.UserAuth;
@Entity
@Table(name="\"UserInfo\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class UserInfo implements Serializable{
private static final long serialVersionUID = 1225151430484658395L;

View File

@ -20,9 +20,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"UserProject\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class UserProject implements Serializable{
private static final long serialVersionUID = -4467370784003784660L;

View File

@ -18,9 +18,7 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="\"UserAuth\"")
//@Proxy(lazy = false)
//@JsonInclude(Include.NON_NULL)
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class UserAuth {
@Id

View File

@ -2,18 +2,27 @@ package helpers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
public class SerializerProvider {
private static ObjectMapper objectMapper = new ObjectMapper()
// .setSerializationInclusion(Include.NON_NULL)
// .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new Hibernate5Module())
;
private static ObjectMapper objectMapper = null;
static {
Hibernate5Module module = new Hibernate5Module();
module.enable(Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS);
objectMapper = new ObjectMapper()
// .setSerializationInclusion(Include.NON_NULL)
// .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(new Hibernate5Module())
;
}
public static ObjectMapper getJsonSerializer() {
return objectMapper;
}

View File

@ -134,6 +134,7 @@ public class DMPs {
}
catch(Exception ex) {
ex.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@ -252,6 +253,20 @@ public class DMPs {
*/
@RequestMapping(method = RequestMethod.POST, value = { "/dmp/getdatasets" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Object> getDatasetsOfDMP(@RequestBody DMP dmp) {
Set<Dataset> datasets = dMPDao.read(dmp.getId()).getDataset();
try {
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(datasets));
} catch (JsonProcessingException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not get datasets of DMP!\"");
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/dmp/delete" }, consumes = "application/json", produces="text/plain")
public @ResponseBody ResponseEntity<Object> delete(@RequestBody DMP dmp) {

View File

@ -72,9 +72,7 @@ public class DatasetProfiles {
@Autowired private ResearcherDao researcherDao;
@Autowired private ServiceDao serviceDao;
private ObjectMapper objectMapper =
new ObjectMapper();
//SerializerProvider.getJsonSerializer();
private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer();
//FETCH BY DATASET PROFILE
@ -102,6 +100,31 @@ public class DatasetProfiles {
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/datasetprofile/getAll" })
public @ResponseBody ResponseEntity<Object> getAllDatasetProfiles(){
try {
List<DatasetProfile> allDatasetProfiles = datasetProfileDao.getAll();
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
List<String> datasetProfilesStrL = allDatasetProfiles.stream().map((datasetProfileObj) -> {
try {
return objectMapper.writeValueAsString(datasetProfileObj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return "";
}
}).collect(Collectors.toList());
return new ResponseEntity<Object>("["+String.join(",", datasetProfilesStrL)+"]", HttpStatus.OK);
}
catch(Exception ex) {
ex.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Transactional
@ -113,14 +136,14 @@ public class DatasetProfiles {
if(datasetprofileruleset != null) {
if(datasetprofileruleset.getId()==null)
datasetprofileruleset = datasetProfileRulesetDao.create(datasetprofileruleset);
datasetProfileRulesetDao.create(datasetprofileruleset);
else
datasetProfileRulesetDao.update(datasetprofileruleset);
datasetProfile.setRuleset(datasetprofileruleset);
}
if(datasetprofileviewstyle != null) {
if(datasetprofileviewstyle.getId()==null)
datasetprofileviewstyle = datasetProfileViewstyleDao.create(datasetprofileviewstyle);
datasetProfileViewstyleDao.create(datasetprofileviewstyle);
else
datasetProfileViewstyleDao.update(datasetprofileviewstyle);
datasetProfile.setViewstyle(datasetprofileviewstyle);
@ -129,7 +152,7 @@ public class DatasetProfiles {
try {
if(datasetProfile.getId()==null)
datasetProfile = datasetProfileDao.create(datasetProfile);
datasetProfileDao.create(datasetProfile);
else
datasetProfileDao.update(datasetProfile);
return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(datasetProfile));
@ -177,80 +200,26 @@ public class DatasetProfiles {
}
// //@Transactional
// @RequestMapping(method = RequestMethod.POST, value = { "/datasetprofile/set" }, consumes = "application/json", produces="application/json")
// public @ResponseBody ResponseEntity<Object> setDatasetProfile(@RequestBody DatasetProfile datasetProfile) {
//
// System.out.println("inside setDatasetProfile");
// try {
// System.out.println(objectMapper.writeValueAsString(datasetProfile));
// } catch (JsonProcessingException e) {
// e.printStackTrace();
// }
//
// if(datasetProfile.getRuleset()!=null && datasetProfile.getRuleset().getId()!=null){
// DatasetProfileRuleset dpr = datasetProfile.getRuleset();
// datasetProfileRulesetDao.update(dpr);
// DatasetProfileRuleset n = new DatasetProfileRuleset();
// n.setId(dpr.getId());
// datasetProfile.setRuleset(n);
// }
//
// if(datasetProfile.getViewstyle()!=null && datasetProfile.getViewstyle().getId()!=null){
// DatasetProfileViewstyle dpv = datasetProfile.getViewstyle();
// datasetProfileViewstyleDao.update(dpv);
// DatasetProfileViewstyle n = new DatasetProfileViewstyle();
// n.setId(dpv.getId());
// datasetProfile.setViewstyle(n);
// }
//
// datasetProfile.setDataset(null);
//
// DatasetProfile createdDatasetProfile = datasetProfileDao.update(datasetProfile);
// try {
// return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdDatasetProfile));
// } catch (JsonProcessingException e) {
// e.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not set dataset profile!\"}");
// }
//
// }
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/datasetprofile/delete" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Object> deleteDatasetProfile(@RequestBody DatasetProfile datasetProfile) {
DatasetProfileRuleset datasetprofileruleset = datasetProfile.getRuleset();
DatasetProfileViewstyle datasetprofileviewstyle = datasetProfile.getViewstyle();
try {
if(datasetprofileruleset != null)
datasetProfileRulesetDao.delete(datasetprofileruleset);
if(datasetprofileviewstyle != null)
datasetProfileViewstyleDao.delete(datasetprofileviewstyle);
datasetProfileDao.delete(datasetProfile);
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(datasetProfile));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete dataset profile!\"}");
}
// @RequestMapping(method = RequestMethod.POST, value = { "/createEmptyDatasetProfile" })
// public @ResponseBody ResponseEntity<Object> createEmptyDatasetProfile(@RequestParam("datasetID") String datasetID) {
//
// DatasetProfileRuleset dpr = new DatasetProfileRuleset();
// dpr.setLabel("");
// dpr.setDefinition("");
// dpr.setId(datasetProfileRulesetDao.create(dpr).getId());
//
// DatasetProfileViewstyle dpv = new DatasetProfileViewstyle();
// dpv.setLabel("");
// dpv.setDefinition("");
// dpv.setId(datasetProfileViewstyleDao.create(dpv).getId());
//
// DatasetProfile datasetProfile = new DatasetProfile();
// datasetProfile.setLabel("");
// datasetProfile.setDefinition("");
// datasetProfile.setRuleset(dpr);
// datasetProfile.setViewstyle(dpv);
//
// Dataset ds = new Dataset();
// ds.setId(UUID.fromString(datasetID));
// Set<Dataset> datasets = new HashSet<Dataset>();
// datasets.add(ds);
// datasetProfile.setDataset(datasets);
//
// try {
// datasetProfile = datasetProfileDao.create(datasetProfile);
// return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(datasetProfile));
// }
// catch(Exception e) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED: reason: "+e.getMessage());
// }
// }
}

View File

@ -74,7 +74,6 @@ public class Datasets {
private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer();
// FETCH BY DATASET(S)
@ -111,11 +110,13 @@ public class Datasets {
try {
List<Dataset> allDatasets = datasetDao.getAll();
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
List<String> datasetsStrL = allDatasets.parallelStream().map((datasetObj) -> {
List<String> datasetsStrL = allDatasets.stream().map((datasetObj) -> {
try {
return objectMapper.writeValueAsString(datasetObj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return "";
}
}).collect(Collectors.toList());
@ -123,6 +124,7 @@ public class Datasets {
return new ResponseEntity<Object>("["+String.join(",", datasetsStrL)+"]", HttpStatus.OK);
}
catch(Exception ex) {
ex.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ -219,33 +221,5 @@ public class Datasets {
/*
@RequestMapping(method = RequestMethod.GET, value = { "/createEmptyDataset" })
public @ResponseBody ResponseEntity<Object> createEmptyDataset() {
Dataset dataset = new Dataset();
dataset.setLabel("");
try {
dataset = datasetDao.create(dataset);
}
catch(Exception e) {
e.printStackTrace();
try {
dataset = datasetDao.update(dataset);
}
catch(Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED: reason: "+e.getMessage());
}
}
return ResponseEntity.status(HttpStatus.CREATED).body("Created Dataset with id: " + dataset.getId());
}
*/
}

View File

@ -84,25 +84,25 @@ public class Projects {
// MANAGE PROJECT(S)
@RequestMapping(method = RequestMethod.GET, value = { "/projects" })
@RequestMapping(method = RequestMethod.GET, value = { "/projects" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> listProjects(){
try {
List<UUID> allIDs = projectDao.listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs));
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/projects/{id}" })
@RequestMapping(method = RequestMethod.GET, value = { "/projects/{id}" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getProject(@PathVariable("id") String id) {
try {
Project project = projectDao.read(UUID.fromString(id));
System.out.println(project.getId().toString());
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(project));
return ResponseEntity.status(HttpStatus.OK).body(project);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
@ -110,11 +110,11 @@ public class Projects {
}
@RequestMapping(method = RequestMethod.GET, value = { "/project/listAllLabelIDs" })
@RequestMapping(method = RequestMethod.GET, value = { "/project/listAllLabelIDs" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> listLabelIds(){
try {
List<IDLabelPair> allIDs = projectDao.listAllIDsLabels();
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs));
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
@ -150,15 +150,11 @@ public class Projects {
@RequestMapping(method = RequestMethod.POST, value = { "/project/create" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Object> setProject(@RequestBody Project project) {
Project createdProject = projectDao.update(project);
try {
return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdProject));
} catch (JsonProcessingException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create Project!\"}");
}
return ResponseEntity.status(HttpStatus.CREATED).body(createdProject);
}
@RequestMapping(method = RequestMethod.POST, value = { "/project/delete" }, consumes = "application/json", produces="text/plain")
@RequestMapping(method = RequestMethod.POST, value = { "/project/delete" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Object> delete(@RequestBody Project project) {
Project p = new Project();

View File

@ -1,5 +1,6 @@
package rest.entities;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@ -70,7 +71,9 @@ public class Services {
@Autowired private ServiceDao serviceDao;
private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer();
private ObjectMapper objectMapper =
SerializerProvider.getJsonSerializer();
// new ObjectMapper();
// MANAGE SERVICE(S)
@ -124,7 +127,16 @@ public class Services {
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/service/create" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Object> setService(@RequestBody Service service) {
public @ResponseBody ResponseEntity<Object> setService(@RequestBody String serviceSTR) {
System.out.println(serviceSTR);
Service service = null;
try {
service = objectMapper.readValue(serviceSTR, Service.class);
} catch (IOException e1) {
e1.printStackTrace();
}
Service createdService = serviceDao.update(service);
try {
return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(createdService));
@ -137,10 +149,10 @@ public class Services {
@RequestMapping(method = RequestMethod.POST, value = { "/service/delete" }, consumes = "application/json", produces="text/plain")
public @ResponseBody ResponseEntity<Object> delete(@RequestBody Service service) {
Service s = new Service();
s.setId(service.getId());
System.out.println(service);
try {
serviceDao.delete(s);
serviceDao.delete(service);
return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted Service entity!\"}");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Service entity!\"}");

View File

@ -112,6 +112,7 @@
<context:component-scan base-package="dao" />
<context:component-scan base-package="core" />
<context:component-scan base-package="rest" />
<context:component-scan base-package="configs" />
<!-- <context:component-scan base-package="controller" /> -->

View File

@ -1,18 +1,24 @@
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import dao.entities.DMPDao;
import dao.entities.DMPProfileDao;
@ -30,11 +36,13 @@ import dao.entities.UserInfoDao;
import entities.DatasetProfile;
import entities.DatasetProfileRuleset;
import entities.DatasetProfileViewstyle;
import entities.Service;
import helpers.SerializerProvider;
import rest.entities.DMPs;
import rest.entities.DatasetProfiles;
import rest.entities.Datasets;
import rest.entities.Projects;
import rest.entities.Services;
@ComponentScan(basePackages = "dao, entities, controller, login, proxy, rest")
public class TestRest {
@ -53,7 +61,8 @@ public class TestRest {
@Autowired private ServiceDao serviceDao;
@Autowired private UserInfoDao userInfoDao;
private static ObjectMapper objectMapper = SerializerProvider.getJsonSerializer();
Gson gson = new Gson();
private static String userID = "0f51f686-a2ce-47c1-9433-00736787aa88";
@ -63,6 +72,7 @@ public class TestRest {
private Datasets datasetsService;
private DatasetProfiles datasetProfilesService;
private DMPs dmpsService;
private Services servicesService;
@Before
@ -76,11 +86,11 @@ public class TestRest {
datasetsService = context.getBean(Datasets.class);
datasetProfilesService = context.getBean(DatasetProfiles.class);
dmpsService = context.getBean(DMPs.class);
servicesService = context.getBean(Services.class);
}
@Test
// @Test
public void testDatasetProfile() {
// DatasetProfile datasetProfile = new DatasetProfile();
// datasetProfile.setLabel("Sample-Dataset-Profile");
@ -95,25 +105,58 @@ public class TestRest {
// dpv.setLabel("Sample-Dataset-Profile viewstyle");
// dpv.setDefinition("dpv definition");
// datasetProfile.setViewstyle(dpv);
//
// datasetProfilesService.createDatasetProfile(datasetProfile);
//
// assertNotNull(datasetProfile.getId());
}
// String profJSON = (String)datasetProfilesService.createDatasetProfile(datasetProfile).getBody();
// DatasetProfile prof = null;
try {
// prof = objectMapper.readValue(profJSON, DatasetProfile.class);
// assertNotNull(prof.getId());
}
catch(Exception ex) {
}
// assertNotNull(prof);
// @Test
public void testDatasetProfile2() {
// DatasetProfile datasetProfile = new DatasetProfile();
// datasetProfile.setLabel("Sample-Dataset-Profile-222");
// datasetProfile.setDefinition("Sample-Dataset-Profile-222 definition");
//
// DatasetProfileRuleset dpr = new DatasetProfileRuleset();
// dpr.setLabel("Sample-Dataset-Profile-222 ruleset");
// dpr.setDefinition("dpr definition");
// datasetProfile.setRuleset(dpr);
//
// DatasetProfileViewstyle dpv = new DatasetProfileViewstyle();
// dpv.setLabel("Sample-Dataset-Profile-222 viewstyle");
// dpv.setDefinition("dpv definition");
// datasetProfile.setViewstyle(dpv);
//
// ResponseEntity<Object> createResp = datasetProfilesService.createDatasetProfile(datasetProfile);
// assertEquals(201, createResp.getStatusCodeValue());
//
// datasetProfile.setLabel("SAMPLE-DATASET-PROFILE-222");
// dpr.setLabel("SAMPLE-DATASET-PROFILE-222");
// dpv.setLabel("SAMPLE-DATASET-PROFILE-222");
//
// ResponseEntity<Object> updResp = datasetProfilesService.updateDatasetProfile(datasetProfile);
// assertEquals(201, updResp.getStatusCodeValue());
//
// ResponseEntity<Object> delResp = datasetProfilesService.deleteDatasetProfile(datasetProfile);
// assertEquals(200, delResp.getStatusCodeValue());
}
// @Test
public void testServiceService() {
Service service = new Service();
service.setId(UUID.fromString("37acad15-39a1-46f3-a6f3-1bf0eaefaadc"));
ResponseEntity<Object> delResp = servicesService.delete(service);
assertEquals(201, delResp.getStatusCodeValue());
}
@Test
// @Test
public void testProject() {
System.out.println(projectsService.listProjects().getBody());