2

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

3
  • Take another JSON object and put it in that with adding "" to it. Commented Dec 4, 2015 at 11:52
  • 1
    Why do you want to change it to a String? Commented Dec 4, 2015 at 11:56
  • @Aakash Thanks I have updated the question with the reason to be a String. Commented Dec 4, 2015 at 12:25

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I have updated the question with the reason to be a String.
I think I've managed to answer your angular JS question in it's own thread. I think for the time being just to get something working it's worthwhile just creating a property 'entityTypeIdAsString' which just returns that long as a String and plumb that through to your page, then see if it works before thinking of any neater ways to do it.
0

I think you should make 'entityTypeId' as String instead of long.

1 Comment

Thanks that's a nice and easy fix, but unfortunately we can't change that as it needs to match the DB.
0

I suggest to use a DTO which contains the data as String, and do the conversion in the service layer.

Comments

0

I think you should try this. json = json.replace (/:(\d+)([,}])/g, ':"$1"$2'); If you have used json library.

Comments

0

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.

Comments

0

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>

JSFiddle

I've also updated your other question. Let me know if this is what you were looking for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.