I work on REST service using Spring MVC 3.1. Names of some of the object fields are customized. Also I use JSR-303 validation. For example,
#Valid
#JsonProperty("env_vars")
private List<EnvironmentVariable> env;
Works great, but there is an issue here: error messages contains names of Java fields. I mean if user produces invalid value in field *env_vars*, he get an error message that env field contains an error and it can be confusing.
Is there a way to keep names customization without providing full messages for each field?
Not sure what you mean by "without providing full messages for each field"...
You need to specify the custom field name somewhere, so it may as well use the i18n mechanism for JSR303 - ValidationMessages.properties (see https://stackoverflow.com/a/5781678/249327).
Related
I am having trouble to understand how I should use postman. I am trying to write a schema to validate my test collection. Doing this I ran into a few problems:
Name/Description validation: In our test collection we have several requests to the same path, with different names. For example we have the path {{baseUrl}}/user/{id}. With this path we can create(POST) several types of users. Each request needs to be tested and has its own name (create childUser, create parentUser, create masterUser, etc).
The problem now is, when validating with the schema we get issues for every request name and description. When I remove the name from the schema, it takes the path as name which also shows issues when validating.
I think I am using it wrong. How can I identify each test request uniquely without destroying the validation?
Also when validating my API under "Test" I get "Issues found" but when I click on it the list is empty.. This is very confusing.
How do you use schema validation properly with a big test collection?
Also, the schema documentation is not generating properly and is not showing any errors. It just generates the title and server list. Any clue anyone? Also when I don't add a Collection Documentation it doesn't even show up.
Thanks in advance
John
I'm attempting to define some relationships between entity values, using metadata.
I've uploaded metadata to an entity value using the Watson API v1. When I list the entities through the same API I can see the metadata. I have not been able to access the metadata from Watson Assistant though.
As a test I changed the entity value and checked through Watson Assistant it was changed, so I know I'm working with the correct workspace. I've also checked the entity using the JSON editor to verify it was defined and tried assigning the entity to a context variable.
I've tried several methods including:
#Room.building, #Room.metadata.building, entities['Room']?metadata?.building, entities['Room']?.building (all within the < ? tags). Using #Room and entities['Room']?.value returns the entity value correctly. It's accessing the metadata where it fails.
The metadata for building should not be returning null, but I"m either receiving a null or, depending on how I'm attempting to retrieve the value, a SPEL exception - no property on undefined.
Unfortunately, it is not possible to access metadata defined on user entities in the WA runtime at this moment. The metadata are only returned when making the direct API call. You could use cloud functions call though to get the metadata in WA although that is not ideal either.
I want to set an arbitrary attribute for rendering to JSON.
I had followed the answer in this question: how to append data to json in ruby/rails? to do
model = Model.find
model[:extra_info] = "More detail."
model.to_json
It works perfectly, but in my tests I'm getting a deprecation warning that setting arbitrary attributes is no longer supported, use attr_writer.
I tried using
model.write_attribute(:extra_info, "More detail.")
which works in unit testing, but on the server, raises an exception:
private method `write_attribute' called for Model
What's the non-deprecated clean way to do this.
I'm aware I could set it in the JSON call with methods as in Add virtual attribute to json output, but in this case the variable to be added is not part of the models concern, so it doesn't have access to the data needed to construct the extra attribute, and it would be nasty and messy to do so.
So what's the correct way for the controller to get this data pushed into the model so the JSON renders properly?
In Model model, put
attr_accessor :extra_info
Then in controller
model.extra_info = "more detail"
Nick's answer above is the best in terms of creating well structured, well documented code.
Update
*It seems I was wrong on the below*
The code below still creates deprecation warnings on my development server.
In this particular case, I don't want to clutter the model up with extra accessors for very specific once off cases, so I'm using
s.send(:write_attribute, :extra_info, "more detail")
Inside a helper, inside the controller.
I am developing a application using Spring MVC 3.0 frame work, I have following requirement,
There are multiple form in one jsp page. I am using ajax to submit each form. But after submitting, each form will go to different controller. In controller I will validate input data if there is any error I need to send validation result back to jsp page. Right now I am storing errors into a separate list and sending back to jsp through json response. I am not sure whether to use bindingResult.getAllErrors() or bindingResult.getFieldErrors() to get list of errors in my controller. What's the difference between both?
getAllErrors()
By using bindingResult.getAllErrors you will get all errors, both
global and field ones.
getFieldErrors()
By using bindingResult.getFieldErrors() you will get all errors associated with the given field.
Here is a useful Link that may help you understand difference between each better.
getAllErrors() returns all errors, both Global and Field. getFieldErrors() only returns errors related to binding field values. I am not sure what a "Global" error is generated from, as I have never seen one.
I am getting following exception on struts2 json...any ideas?
I am using Hibernate: 3.6.1, and struts2 with json plugin.
Exception: java.lang.NoSuchMethodException: com.model.Task.getHibernateLazyInitializer()
This is a pain... Hibernate/JPA decorates the entities so that they can do their magic. Long story short you need to prune the entity before returning it as json using exclude properties.
Easiest way is to examine the object at run time, you'll find the offending property then create an exclude regex to prevent the json plugin from serialization.
As a sanity test you can prove there is a bad property by simply defining include properties for the properties you know to exist, which will produce the object you need although it makes what should be an automatic process a pretty manual one, where fining the right exclude property aught to be the same between all hibernate entities.
For examples of include and exclude parameters see: http://struts.apache.org/2.2.3/docs/json-plugin.html