Integrating JackSon for JSON View with Spring 2.5 - java

We are using Spring 2.5 and spring-json for JSON support. We have the below configuration in views.xml
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
Now, we have learnt that Spring 3.x uses JackSon API internally for JSON support. My question is how can we override the default implementation of Spring 2.5 to use JackSon - The way Spring 3.x begins.
Note: We don't want to migrate my Spring version, but, want Spring 2.5 to use this JackSon API instead of Spring-Json

Is it possible to replace Spring's JSON support without breaking it ?
Unfortunately, Spring's own documentation states that Spring-json is "deeply" a component of the existing Spring 2.5 framework.
See : http://spring-json.sourceforge.net/
That said - removing the dependencies on spring-json, adding your own JSON parser, and rebuilding spring can be done. I assume this will require a lot of work given that spring-json is a major component of the whole Spring MVC suite.
An alternative : Building a Facade
In addition, I don't know of any Java EE specification for Json libraries which implies that there is a good chance that all internal Spring json dependencies are specific to the APIs defined by Spring-json [compare this, for example, with JPA, which is generically defined by Java EE, so that it is easy to replace many a DAO framework].
Generally, you can package any sort of JSon library as a Spring component that will be available in the application context. Now - if you reimplement the necessary interfaces using the facade pattern, using Jackson under the hood, your version of Spring 2.5 should work the same. Alternatively, you could intercept Json related calls of interest using Spring's aspect oriented injection libraries, and reroute them as necessary.
Again, however, these are all advanced tasks - they would be excellent learning projects but I'm not sure that the time investment would really pay off if this is a production application.
http://www.javaworld.com/javaworld/jw-02-2008/jw-02-springcomponents.html

Related

Create a SOAP-producer without spring Boot but Spring integration

I have a WSDL-file, which describes properties of a SOAP-requests (XSD-parts and also service-parts). Using Maven, I was able to generate model classes.
Now I want to use given classes to provide some SOAP-services.
Most of tutorials using Spring Boot, but I don't want. They also use a given wsdl-file and xsd-file, which I don't have.
All I want, is to have a easy to code SOAP endpoint, which uses Spring integration, where I can use stuff link #Inject. Do you have an idea?

How to migrate spring annotation-based Hibernate code to Java EE

First of all: here is my code: https://github.com/s1ddok/todosapp
I'm learning java technologies. I forked a demo project on github and for now my goal is to add login (user specific todos) and hibernate mysql stuff. I successfully done it for -spring version. Now I want to do the same stuff for java ee.
How do I migrate annotation based hibernate code (Entities and config) to java ee? Is it possible to simply reuse them? I heard that you have to use xml config with Java EE, is it true?
My requirement is to use same front-end (backbone) and same database for both -spring and -javaee. This is my goal. I have to implement same-functionality REST service with spring and javaee.
I'm completely stuck with that.
With JavaEE you do not have to use much XML configs (in comparison with the old J2EE), but you can do most things with annotations.
Regarding database access you can still use Hibernate (JPA), since JPA is part of the JavaEE spec. The Json annotations like com.fasterxml.jackson.annotation.JsonIdentityInfo are not part of JavaEE spec (yet), so you might have to use JAXB instead.
Regarding REST you want to have a look at JAX-RS. The annotations are different in comparison to Spring (#ApplicationPath, #Path, #GET, ...), but you should be able to achieve the same result for you application.
For security (login) you have to look at application server specific options, since this be will standarized only in JavaEE 8 (JSR 375).

Creating java RESTful application

I want to create new RESTful application. I am bit confused about framework I can do it with spring+jersey , but can I do same application using jersey alone?
What is major difference between SpringREST and jersey?
Which is more convenient? why?
I've used both frameworks a bit. Spring is a large framework/API that covers many areas, one of which is rest services. Jersey on the other hand just covers rest. It's the reference implementation for the JAX-RS API (JSR 311 & JSR 339).
This is basically the "standard" way to do rest in Java. There are also other implementations like RestEasy. In theory your code will only need to reference the common JAX-RS interfaces meaning you ought to be able to swap to a different implementation later if required. This obviously only works if you don't become reliant on bespoke functionality that isn't part of the JAX-RS standard.
If you were to use Jersey, you might still decide you want spring. It can be useful just for its dependency injection alone. In this case you might have a JAX-RS class handling rest requests which then calls a spring service which has been injected. This is actually how I'm writing rest API's.
Whether you should use spring to write the rest services or JAX-RS is subjective and really up to you. Personally I went with the standard JAX-RS API because I found it was more focused on rest. The spring rest approach is basically an extension of spring-mvc which was originally intended for JSP's. I found things like error handling were easier using JAX-RS than spring-mvc. That said someone else may beg to differ. The other benefit is by following the standard in theory you have more flexibility in future if you want to switch to a different provider.
The main difference is that Jersey is standards-based, and Spring MVC is not, if that matters to you. Both are very good.
The main advantage I found in Jersey (I used 1.x) was that it could automatically use Jackson JSON Views automatically, and Spring MVC could not. Also, error handling in Spring MVC is kind of irritating, as error pages default to standard HTML.
There is another project you have not listed, and that is Spring Data + Spring HATEOAS, which is newer, but seems pretty good.

Does spring have a validation framework module that can be used in any layer for validatons?

For instance, I am using JSF + custom framework developed in our company. Now I want to use a third party validation framework that can be used as an plug-in and it should not create any dependency what ever may be the technical stack.
So my question is does spring provide any framework of that sort or if it's available how can I use that?
I am expecting a validation framework something like, which is configurable through XML.
Spring does have a validation framework, but if you want minimal dependencies, then I'd suggest that you go with a Bean validation provider. It's a new(ish) official validation standard, defined in JSR-303.
There are several implementations at the moment. I'd give Hibernate Validator a look.
I disagree. Hibernate Validator is an awful piece of software (at least the versions that were current about a year ago). Spring Validation is a nice piece of software, that goes together well with the BeanWrapper interface.
But it's true: Spring Hibernate resides inside the Spring Context jar, which is unnecessary overhead. Hopefully there will be a separate version sometime.

Does Java Spring 3.0 MVC support annotation/attribute based client side validation like Asp.net MVC 2.0?

In Asp.Net MVC 2.0, at least in the beta, you could decoration your model classes with data annotation attributes and enable client side validation that leverages that criteria defined in your model data annotation attibutes. Is there anything similar for Java Spring MVC 3.0?
Is it possible to inject a component into the response pipeline that can inspect the model's annotated properties and render client side validation logic to complement the server side validation logic that is invoked prior to the controller handling the request?
Actually, there is something a little bit like it, based on the JSR-303 Bean Validation spec, which is now final and fully supported by Spring 3.0.
Recently, as part of Spring Webflow, Springsource has released Spring JS (javascript) which uses Dojo. You can use that for your client side validation. Check out this howto
Obviously, component-frameworks such as Wicket (like fraido mentioned) have better support for this kind of usecase. So if you have to implement a lot of similar usecases, that is probably a better fit for your project. However, if it is sporadic, Spring JS / Bean validation could be the way to go.
Spring MVC is a fairly low level framework. It doesn't extend to doing client side validation.
There are other Java Web frameworks that do this such as Tapestry, Wicket and JSF (IceFaces, etc). These are what I tend to call "component frameworks".
In Spring MVC 3.0 there's nothing like that at the moment. As cletus said frameworks like Tapestry (ex), Wicket (ex) and others have some sort of Client Validation that uses JavaScript to validate forms etc.
You can write your own validation Jstl taglib maybe with the help of a JS Library (jQuery, ...) and Plugins like : jQuery Validation Plugin
edit: I've just found this: Mediawidget. Maybe it's worth having a look.
It says: "Metawidget reads Bean Validation annotations and generates forms with
widgets that are pre-configured for minimum/maximum values, lengths etc."
Spring integration Link1, Link2
This project looks to be exactly what you're looking for: http://kenai.com/projects/jsr303js
2011-12-06: I'm just about to try it myself
2013-12-09: Update on this answer: I did try the library out, and it worked very well. It needed some tweaking (it's a prototype patch for Spring, not a finished plugin), but it was the best solution I could find at the time. Perhaps there is a new solution, or perhaps someone has finished off this prototype since I posted? If not, I can recommend this one.
Ok, this is a variation on what's already been said, but one of the big strengths of Spring-MVC is its ability to integrate with other libraries, like the different JSF variations that do give client side validation.

Categories

Resources