I am getting a weird exception with a trouble on non-existing field either on my response or request. I have configuration setup to generate snippets for response/requests on my endpoints with documentation on the fields of those beans. Except I am getting the following exception from org.springframework.restdocs.mustache package;
MustacheException$Context: No method or field with name 'required' on line 6
I have no 'required' field declared anywhere on my descriptor snippets, or within my request/response beans, and what is the deal with line 6? What a mysterious exception! What is the issue here?
The trouble stemmed from a custom request-parameters template, in src/test/resources/org/springframework/restdocs/templates/ resource folder. Due to the addition of a custom field within the template of request descriptor, the lack of this field was causing the issue. List 6 was where the 'request' field was being used within this file, thus the error.
Removing the custom template to let REST Docs use its default template resolved the issue.
Related
I have a Spring application with a JPA Repository. Now I'd like to add some validations. I found several solutions but none works perfect and I don't know which methode should be used in which case:
1.) Im using validation annotations (e.g. #NotNull) in my model object. But this generates a not usefull rest error response like ""Internal Server Error","message":"Could not commit JPA transaction; "
2.) I'm using the 'Validator' interface for custom validations but I get the spring "compiler" error "Validator has incorrect spelling"
3.) Some tutorials use 'ConstraintValidator' interface
4.) Some tutorials write custom rest methods for validations.
When should I use which and how can I solve my problems?
This is how you can manage it and easy is:
Define all your custom message in /messages/messages.properties under resources folder.
so if you error property is: error.user.name = User name can not be null.
then call your specific property in your pojo for that property.
#NotNull(message = "error.user.name")
Consider a Person entity with a property name that is annotated as #NotNull. Then a simple PersonRepository and this repo exposed with Spring Data Rest.
When I POST to create a new Person, if the name property is null a ValidationException occurs as expected. But what I actually get on the client is an Internal Server Error (500) and the message is a TransactionSystemException that happened much later in the exception chain.
What I'd expect to get is a Bad Request (400) with the actual ValidationException and all it's useful information so the client can know what's wrong with the posted data.
There seems to be a way to attach custom validators with SDR as explained here. But the thing is, this is not a custom validator, it's a standard bean validation that happens when the repository is asked to save data. So I'm not really sure how those two come together.
So questions:
What are my options to let the client know what's wrong with the submitted data when using SDR?. Things like what fields are invalid and what's the error for each field would be awesome.
Are there any examples about this anywhere?
Thanks a lot.
What you need is a proper ExceptionHandler, it will handle back end exceptions and send meaningful rich messages (json/xml) to the front end client.
Take a look a this git repository
I have error_messages table, which contains the site-wide error messages.
I use the error messages across application. So, I created singleton bean of error messages (ErrorMessagesLoad.java)
ErrorMessagesLoad uses ErrorMessageDao to retrieve the error messages from database.
Should I create static variable in ErrorMessagesLoad to hold all the error messages and use it in all classes? or is there better way of doing it?
Thanks,
Satya
You should use your own implementation of MessageSource in Spring to resolve any messages. Here is a good point to start.
When implemented, you just wire your bean to any service or controller and it handles messages for you, with ability to iternationalize them.
In general global static variables should be avoided. Error handling can get tricky. Many applications try to put a global catch handler somewhere near the top (e.g. web applications the top would be filters) which has the ErrorMessagesLoad injected into it. That handler catches underlying exceptions, translates them into something user readable and then throws that higher.
Some examples include Spring's exception translation filter and Jersey's exception mapping mechanism.
Using Play 1.2.3 I am trying to implement the Secure Module across multiple controllers.
I have added - play -> secure to my dependencies and the secure module appears in my project. I have imported the default secure routes * / module:secure, customised the authenitcation method and annotated all of my controllers using the #With(Secure.class).
The problem that I am having is that when I move between controllers I receive a Null Pointer Exception thrown from the secure:module at line 193: return Java.invokeStaticOrParent(security, m, args);
A second issue that I am having is that when methods are called from within the same controller, some calls can take up to 20 seconds to complete where they would normally be instant without the secure module included. Edit: this was unrelated - the secure module has no visible effect on loading time
My question is has anyone else implemented the secure module in Play across multiple controllers, and if so, did they come across any of these issues?
Edit
The problem was down to the use of a tag in my template - not in the implementation of the secure module. See below for reason and how to resolve.
The reason the null pointer exception was being thrown was because in my template I was calling:
#{secure.check}
<li>Logout</li>
#{/secure.check}
I had oddly thought this was a template tag to check whether there was security enabled - but it actually needs be followed by a 'profile' type - which is not something I specified nor have implemented (hence the exception).
To get around this, I added a basic template tag that checks whether the security associated session is in use:
#{if session.username}
#{doBody /}
#{/if}
And can simply implement as follows:
#{secure.secure}
<li>Logout</li>
#{/secure.secure}
I got this exception in time of running a web application in java. What does this mean?
exception.name = javax.servlet.ServletException: BeanUtils.populate
I guess you are using something which utilizes Jakarta BeanUtils (like Struts) and some method is throwing an exception.
Following may be reasons for same :
The action attribute of an tag must
match exactly the path attribute of
the action definition in the
struts-config.xml file. This is how
Struts associates the ActionForm
bean with the action.
This error usually occurs when you
have specified a form name that does
not exist in your tag. For example,
you specifiec and 'myForm' is not
the name of a form associated with
myAction in the struts-config file
You get this message when Struts is
unable to map the data in the HTML
form to the properties in your
ActionForm bean. Make sure each of
the properties on your bean is
either a String or a boolean. Do you
have any properties of type
java.util.Date or other objects?
That might cause this error. Also
check to see that you have public
getters and setter for each of your
properties.
Check:
http://www.coderanch.com/t/53114/Struts/ServletException-BeanUtils-populate
http://forums.sun.com/thread.jspa?threadID=632599
http://javaexceptions1.blogspot.com/2009/08/javaxservletservletexception.html
A short call to google's famous www-indexer (with:"ServletException: BeanUtils.populate") provided this result:
ServletException BeanUtils populate
The answer to that question over there at coderanch could help to solve your problem
Since this is a Struts related exception (and seeing that we don't know the cause of the exception), here are some possible reasons why you're getting the exception.
No Bean Specified. This means that there is no ActionForm defined in your Action.
Your bean properties you're copying from doesn't match the bean properties you're matching to.
Unless we know the cause of the exception, you'll just have to debug your code and see what is the fault.