I want to set null value to entity by sending null request.
For example:
PATCH: "{deleteDate: null}" to http://localhost/api/entity/1
But it doesn't work.
I found here information how PATCH requests processed:
An new instance of Foo is created
Foo is populated with all values that have been sent with the request
The Foo entity with the id provided by the URI is loaded
All properties that differ between the two objects are copied from the new Foo to the persisted Foo, unless the value is null in the new Foo.
Do I understand correctly that it is impossible to set value to NULL with PATCH request to spring-data-rest service API?
In Spring context null values in PATCH method means that there are no changes.
If you want write null values you can
1) use PUT method;
2) implement your own DomainObjectMerger class, in which you can extend method merge with condition like
sourceValue != targetValue;
3) use DomainObjectMerger.NullHandlingPolicy configuration.
Depends on your Spring Data REST version.
All 3 options from egorlitvinenko's answer will solve the described problem but will have another one:
All other properties which were not specified in PATCH request would be "nullified" too.
Seems like spring-data-rest, issue-345 was already fixed in v2.2.x.
Related
I have an impex file(example)
INSERT_UPDATE Subscriber;firstName[unique=false];lastName[unique=false];email[unique=true];bulk(code)[default=false]
;FirstName;lastName;email#gmail.com;true;
and an InitDefaultInterceptor
public class MyInterceptor implement InitDefaultInterceptor<SubscriberModel>{
onInitDefaults(SubscriberModel model, InterceptorContext ctx)
}
How can I get values from the impex in this interceptor? I try to use
model.getFirstName();
....
but all the methods returns "null". What can I do to get the values ? I need to implement a logic before I save them into db.
If you want to make use of the values that are being sent, you need to use a PrepareInterceptor instead of a InitDefaultInterceptor interceptor.
InitDefaultInterceptor : The Init Defaults Interceptor is called when a model is filled with its default values. In your case, this happens at creation of the new instance of the object you want to add to the database. This interceptor is used to add default values (next to the ones you already defined in your items.xml). Only the defaults, marked in your items.xml are inserted at this point. No data from your impex is loaded here, as this just handles the defaults for new objects, no matter the content that will be added at a later stage.
PrepareInterceptor : The Prepare Interceptor is called before a model is saved to the database. Use this to add values to the model or modify existing ones before they are saved. In this interceptor, the values of your impex will be filled in the model object. You can add or modify your data here depending on your usecase.
For more info on all type of interceptors, there is a help page from SAP that describes all of them.
I am trying to make my #webparam mandatory using (#XmlElement(required=true) but the generated XSD still shows this as minOccurs="0". Also tried setting nillable=false but still not working.
Here is my web method :
#WebMethod
#WebResult(name = "Biller")
public Biller getBiller(#XmlElement(required=true) #WebParam(name = "billerId") Integer billerId){}
Please suggest.
As specified by me in comments - either you need to wrap your Integer variable into some Java POJO and apply rules for specific fields in that POJO or change to primitive type because - reference types are always optional but constituent fields of wrapper types can be made required or optional.
Primitive types are always required.
Refer this answer
Then comes the question about default values - and for that refer Answers to this question and summary is - if nothing is specified -
minOccurs and maxOccurs default to 1
Now why your call succeeds with SOAP UI - Your xsd is correct and that is acknowledged by SOAP UI so my guess is client might be attaching some default values when its missing. I haven't used SOAP UI. Try examining your request ans see if value is really missing. If value is indeed missing in request then try to examine as why validation is not kicking in.
I am using #ProjectedPayload on an interface to bind a JSON request body to a proxied instance of my interface.
My interface is as follows (in Kotlin, the val just translates to a Java getter):
#ProjectedPayload
interface ImportServer {
val id: UUID?
val name: String?
}
Now, the id here is completely optional, but there is no way for me to specify that, at least as far as I can tell. If I try to access the id property without it being present in the request body I will receive an exception from JsonPath: com.jayway.jsonpath.PathNotFoundException: No results for path: $['id']. If I could configure JsonPath I could use Option.DEFAULT_PATH_LEAF_TO_NULL and I'd get null for elements that are not present. But there is no way for me to get at the JsonPath configuration, it happens locked away inside JsonProjectingMethodInterceptorFactory.
Is there a way for me to specify a default here? Or is there a way for me to detect if the id is present without accessing the id property in the first place?
That's a bug and has been fixed with DATACMNS-1145. Will be released with Ingalls SR7 and transitively with Boot 1.5.7.
I have a created a custom field in Contacts object in Salesforce whose API name is "Resume_Text__c" and I'm making a SOAP call to get the value of that filed using Java Implementation by writing a following SOQL.
SELECT Resume_Text__c FROM Contact
But execution of query throwing following exception.
No such column 'Resume_Text__c' on entity 'Contact'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.'
So how can I access custom field via Soap API Java Implementation?
Whenever you are using Enterprise.wsdl file in your implementation, you need to make sure that every time you create some new fields and object on Salesforce.com environment, you refresh your Enterprise.wsdl to import all the dependency mappings else go with Partner.wsdl.
Could anyone please clarify the defination of attribute?
for example, in the following code, what is an attribute:
request.setAttribute("ja",new foo.Employee());
Is the attribute in the above code an object of type foo.Employee(), or it is key/value pair, or it is actually "ja"?
Request attributes are values indexed by a key (in your case "ja") which are shared in the life of the request object. In Java filter, servlet, jsp, include and forward use same request object so for example you can push an object in a servlet and pull it in a JSP.
The same approach is for session and application scopes
Request attributes are (or at least act like) a map of objects, in this case the key is "ja" and the value is a new foo.Employee.
The session, page, and application have the same data structure.
From the servlet API specification:
Attributes are objects associated with a request. Attributes may be set by the
container to express information that otherwise could not be expressed via the API,
or may be set by a servlet to communicate information to another servlet (via the
RequestDispatcher). Only one attribute value may be associated with an attribute name.
Here an attribute is a custom piece of information (here a new foo.Employee) added to your request (in a Map,Object> . This information will last as long as this request is processed and it can be used later in the process, for example by a JSP.
It's a key value pair
From the docs:
setAttribute
public void
setAttribute(java.lang.String name,
java.lang.Object o)
Stores an attribute in this request. Attributes are reset between
requests. This method is most often
used in conjunction with
RequestDispatcher.
Attribute names should follow the same conventions as package names.
Names beginning with java., javax.,
and com.sun.*, are reserved for use by
Sun Microsystems.
If the value passed in is null, the effect is the same as calling
removeAttribute(java.lang.String).