In a Spring-MVC app in my datamodel I've two fields
#JsonView(JsonXml_Views.List.class) private Boolean arrested;
#JsonView(JsonXml_Views.List.class) #JsonInclude(Include.NON_EMPTY) private Integer arrestedindex;
The use case now is, that arrestedindex only should be marshalled when arrested is set to true. But with the annotation #JsonInclude(Include.NON_EMPTY) arrestedindex also will be marshalled when arrested is set to false which I want to prohibit.
At the moment I absolutely have no clue how to implement this.
Anybody able to help me with this?
Thanks in advance
Related
I know this is a very specific ask but I've got a situation where it would be very nice to have my custom class deserialized and have the collections parent ID set to a specific field. I know with the #DocumentId annotation we can do this for the documents own ID, is there some SDK method to extend this for my use case?
public class MyCustomClass {
#DocumentId public documentID;
...
#<insert magic here> public documentsCollectionsParentID
So for example, something like /users/<uid>/docs/<docid>, I already have the functionality for documentID to be set automatically to docID, but I'd like documentsCollectionsParentID set to uid. Is this in any way possible at the moment?
My current alternative is to deserialize, and the the object afterword:
MyCustomClass thing = (MyCustomClass)doc.toObject(MyCustomClass.class);
thing.setDocumentsCollectionsParentID(doc.getReference().getParent().getParent().getId())
Unfortunately, there is no method in Firebase that does that automatically. This is Java limitation and you are doing it correctly by deserializing the object.
This may be a simple task, but I couldn't find a way to do it. Basically, I need to disallow some parameters at the time of using #RequestBody annotation in my controller.
Here is my model:
#Data
public class MyModel {
private int id;
private String name;
}
What I want to do is at the time of response, I want both of the properties to be serialized to JSON, but at the time of create or update, I prefer not to receive id as part of #RequestBody deserialization.
Right now, if I pass id in the JSON body, Spring initializes a MyModel object with its id set to the passed value.
Reason? The ID cannot be generated until the model is created, so the app shouldn't allow the ID to be set. On update, the ID needs to be passed in the URL itself e.g. (PUT /mymodels/43). This helps following the REST principles appropriately.
So, is there any way to achieve this functionality?
Update 1:
Right now, I am stuck with using a request wrapper. I created a new class MyModelRequestWrapper with only name as its property, and have used it with the #RequestBody annotation.
How you do this depends on what version of Jackson you are using. It's basically possible by a combination of the annotations #JsonIgnore and #JsonProperty on relevant fields/getters/setters.
Have a look at the answers here: Only using #JsonIgnore during serialization, but not deserialization
I am trying to save my entity in elasticsearch using spring data elasticsearch, all the attributes are saved (including objects) except for enum its always stored as null, this is my entity
#Entity
#Document(indexName="invoices", type="invoices", shards = 1)
public class Invoice {
#Transient
#JsonIgnore
#org.springframework.data.annotation.Id
private String searchIndex;
#Field(type = FieldType.String)
private InvoiceStateEnum state;
with and without #Field attribute state is being saved as null even though the object is being saved has value for this enum.
Any help is appreciated
As spring-data-elasticsearch uses Jackson, you can put the #JsonFormat.Shape.STRING annotation to your enum:
#JsonFormat.Shape.STRING
public enum InvoiceStateEnum {
// your enum code
}
I was able to solve the issue by removing folder data under my project and rerun the application, seems like for some reason elastic search was not updating the records so I was getting null since the attribute was added recently.
Is it possible to delete entities using an entity's unique attribute?
In Spring Data 1.4.3.RELEASE, adding methods to find by unique attributes is very easy but, I haven't found a way to do it with delete.
In the following code, Spring automagically handles the findByAddress, is there something similar for delete?
Something like void deleteByAddress(String hwAddress);, I have added it to TerminalRepository but it doesn't work.
public Terminal {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long id;
#Column(unique=true)
private String hwAddress;
...
}
public interface TerminalRepository extends
CrudRepository<Terminal, Long> {
Terminal findByAddress(String hwAddress);
}
Of course it is possible to find the entity by address and then use the delete(Terminal) method passing the found entity as parameter. But this wouldn't be good in terms of performance as it will be making one unnecessary call to the database database, i.e., one avoidable call to find the object and another one to delete it
I don't think there's anything built-in for that. You'd have to use the custom method support to roll your own:
http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/repositories.html#repositories.custom-implementations
I was facing the same issue. But, when I annotated method in repository interface with #Modifying , it started working. But, I don't know how it started working. Can anybody explain?
I have searched all over the place and it seems almost all answers for this problem tell me to annotate either my id property or getter with #Id. I have tried both with absolutely no luck. Here is my entire class thats failing: http://pastebin.com/Cpsdx2Rj
and the following is my exception: http://pastebin.com/uhs9e81b
As I said, I already tried moving the #Id devorator to the property and that doesnt seem to do anything at all.
Any suggestions on what to try? Any other configuration files needed to debug?
Thank you in advance for any help you can provide!
You should annotate the variable itself, and not the getter.
Did you try to add #Access(AccessType.PROPERTY) before your class definition to change the default access type from field to getter method
Like this:
import javax.persistence.AccessType;
#Entity
#Access(AccessType.PROPERTY)
public class Comment implements DomainEntity {
private Long id;
#Id
#Column (name="id",unique=true,nullable=false)
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return this.id;
}
.
.
.
}
I did not find a solution to this, but from testing on another computer it was apparent that the version mismatch (another problem for another day) was not causing this. I deleted my workspace, my m2-cache, reimported the project and redownloaded all dependencies and now it suddenly works (Spring version problems and all).
Thank you all for your help.