Reputation: 9509
One of my JSON content has number for entityTypeId, how to change this to a String ?
eg change 1 to "1".
JSON
[
{
entityTypeId: 3,
entityTypeName: "Branch of Legal Entity"
},
{
entityTypeId: 1,
entityTypeName: "Legal Entity"
},
{
entityTypeId: 2,
entityTypeName: "Notional Entity"
}
]
REST API
@GET
@Path(value = "/entityTypes")
@Produces(MediaType.APPLICATION_JSON)
@Override
public List<EntityType> getEntityTypes() {
return commonBusiness.getEntityTypes();
}
JPA Entity
public class EntityType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="ENTITY_TYPE_ID")
private long entityTypeId;
@Column(name="ENTITY_TYPE_NAME")
private String entityTypeName;
Update:
Many of you asked why I need to change to a String. I use this JSON data to render a drop-down. This drop down value (entityTypeId) saves in the DB in a number column successfully. But when I load the view page the drop-down is not loaded with that value. Other drop downs work which has both those values as String.
Earlier I raised a separated issue Angularjs - dropdown - Should the key/value pair always be a string data type
Upvotes: 2
Views: 249
Reputation: 1791
I don't think using number is an issue in a select.
Check out this example I've created.
<div ng-app="myApp" ng-controller="myController">
<select ng-model="entity.selected">
<option value="{{ent.entityTypeId}}" ng-repeat="ent in entityTypes">{{ent.entityTypeName}}</option>
</select>
Selected entity id {{entity.selected}}
</div>
I've also updated your other question. Let me know if this is what you were looking for.
Upvotes: 0
Reputation: 81
If I understand correctly, the requirement here is to have the REST service json response to have entityType as a number. This can be achieved by creating a json response using a custom serializer.
@GET
@Path(value = "/entityTypes")
@Produces(MediaType.APPLICATION_JSON)
@Override
public Response getEntityTypes() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("entityModule",
Version.unknownVersion());
module.addSerializer(EntityCollection.class, new EntityTypeJsonSerializer());
mapper.registerModule(module);
String responseJson = mapper.writeValueAsString(commonBusiness.getEntityTypes());
return Response
.status(Status.OK)
.entity(responseJson)
.type(MediaType.APPLICATION_JSON).build();
}
Create an interim collection class
public class EntityCollection{
private List<EntityType> entityTypes;
}
Custom Serializer:
public class EntityTypeJsonSerializer extends JsonSerializer<EntityCollection> {
@Override
public void serialize(EntityCollection entityTypes, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
// JSON parsing goes here
jgen.writeString(String.valueOf(entityType.get(entityTypeId)));
}
}
This will make your JPA entity and response JSON independent.
Upvotes: 0
Reputation: 109
In your EntityType class you would need to change the type of the entityTypeId to be a String, but there might be an impact if you do that, so you need to think about what that column accepts in the database. The bigger question is why do you want to change your data type to be a String.
Upvotes: 1
Reputation: 144
I think you should try this. json = json.replace (/:(\d+)([,}])/g, ':"$1"$2'); If you have used json library.
Upvotes: 0
Reputation: 408
I suggest to use a DTO which contains the data as String, and do the conversion in the service layer.
Upvotes: 0
Reputation: 405
I think you should make 'entityTypeId' as String instead of long.
Upvotes: 0