To also see the violations logged at the server, we need to turn on the log level of the package
org.springframework.web to debug. To do this, locate the code snippet in the file
src/main/resources/log4j.xml and change the log level from info to debug, as shown in Listing 16-42.
Listing 16-42. Changing Log4j Log Level
<logger name="org.springframework.web">
<level value="debug" />
</logger>
Rebuild the project and deploy to tc Server. Now run the RestfulClientSample class again, and you
will see the following output (other output was omitted):
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad
Request
When it fails validation, Spring MVC will return the HTTP status code 400 automatically, which
indicates that the data in the request body is getting an error. If you take a look at the server output
console, you will see the following output (other output was omitted):
DEBUG: org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver ­ Resolving
exception from handler [public com.apress.prospring3.ch16.domain.Contact
com.apress.prospring3.ch16.web.restful.controller.ContactController.create
(com.apress.prospring3.ch16.domain.Contact)]:
org.springframework.web.method.annotation.support.
MethodArgumentNotValidException: Validation failed for argument at index [0] in method: public
com.apress.prospring3.ch16.domain.Contact
com.apress.prospring3.ch16.web.restful.controller.ContactController.  create(com.apress.prospr
ing3.ch16.domain.Contact), with 1 error(s): [Field error in object 'contact' on field
'firstName': rejected value [JJ]; codes
[Size.contact.firstName,Size.firstName,Size.java.lang.String,Size];
arguments[org.springframework.context.support.DefaultMessageSourceResolvable: codes
[contact.firstName,firstName]; arguments []; default message [firstName],60,3]; default
message [size must be between 3 and 60]]
In the previous output, you can see the JSR-303 validation error message is displayed. In real life,
you should let the client know this error. This can be done by designing a POJO that stores the errors and
then returns them to the client so the client knows what's wrong within the request. We will leave it to
you as an exercise.
Remoting in the Sample Application
In the SpringBlog application, the main remoting functionality is the retrieval of blog posting entries by
the clients. To fulfill the requirement, we will use Spring MVC's comprehensive RESTful-WS support.
The main highlights of the implementation of the blog feed service are as follows:
A RESTful servlet will be defined for remote retrieval of blog posting entries via
·
RESTful-WS.
A controller will be implemented in the web layer to accept requests (with a
·
predefined URL and HTTP GET method) and return the most recent blog posting
entries to the client.
In terms of data formats, JSON and XML will be supported, and the techniques
·
discussed in this chapter will be used in the sample application.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home