Integration of SPRINGFRAMEWORK4 Series Resteasy

Source: Internet
Author: User
Tags jboss

For integration with JBoss AS7 does not need to do any work, jboss default integration of Resteasy, only need to make some jax-rs annotation annotation to the business Pojo.


For servlet containers that are not JBoss

Spring and Resteasy are integrated in three main ways ,

    1. Run on servlet version greater than or equal to 3.0

    2. Run on servlet version less than 3.0

    3. Integrate Resteasy and Spring MVC


Basic ConfigurationBuilding Pom Dependencies
<dependency>    <groupId>org.jboss.resteasy</groupId>     <artifactid>jaxrs-api</artifactid>    <version>${ project.dependency.jboss.resteasy}</version></dependency><dependency>     <groupId>org.jboss.resteasy</groupId>    <artifactId> Resteasy-jaxrs</artifactid>    <version>${project.dependency.jboss.resteasy} </version></dependency><!-- Resteasy  Integration  Jackson2  Toolkit   facilitates json  conversion.  --><dependency>    <groupId>org.jboss.resteasy</groupId>     <artifactId>resteasy-jackson2-provider</artifactId>    < version>${project.dependency.jboss.resteasy}</version></dependency><dependency>     <groupid>org.jboss.resteasy</groupid>    <artifactid>resteasy-jaxb-provider</artifactid>     <version>${project.dependency.jboss.resteasy}</version></dependency><!--  Reasteasy  Integrated SPRING&NBSP;G Toolkit--><dependency>    <groupid> Org.jboss.resteasy</groupid>    <artifactid>resteasy-spring</artifactid>     <version>${project.dependency.jboss.resteasy}</version></dependency>
Web. XML configuration
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: Spring-config.xml</param-value></context-param>
Run on servlet version greater than or equal to 3.0Supported initializers for Servlet 3

If the servlet version is greater than 3.0, then you need to add a servlet initializer

<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId> Resteasy-servlet-initializer</artifactid> <version>${project.dependency.jboss.resteasy}</version ></dependency>
Configuration of the Web. xml file

To add 2 listeners, note that the context Loader Listener, which was originally used by spring, is not used because the second listener has finished loading the spring work.

<!--Start Resteasy--><listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.resteasybootstrap</listener-class></listener><!--Load Spring Context--><listener> <listener-class> Org.jboss.resteasy.plugins.spring.springcontextloaderlistener</listener-class></listener>
Writing the Pojo class
@Path ("/restuser") @Namedpublic class Restuserservice {@Injectprivate userservice userserviceimpl; @GET @Path ("/list") @Produces (value = {Mediatype.application_json}) public String list () {User user = Userserviceimpl.get (4L); return Jacksonutils.get (). ToJson (user);}}

Start the server and access http://ip:port/your project path/restuser/list. You get the JSON data.

Run on servlet version less than 3.0

When the version of the container servlet that runs is less than 3.0 then the Serlvet3 initializer is not working properly and needs to be removed.

Configuration of the Web. xml file

To specify a prefix otherwise, url-pattern conflicts with spring MVC

<servlet>       <servlet-name>resteasy</servlet-name>      <servlet-class> org.jboss.resteasy.plugins.server.servlet.httpservletdispatcher</servlet-class>       <context-param>            < param-name>resteasy.servlet.mapping.prefix</param-name>             <param-value>/rs</param-value>         </context-param>    </servlet>       <servlet-mapping>      <servlet-name>resteasy</servlet-name>       <url-pattern>/rs/*</url-pattern>    </ Servlet-mapping> 

Start the server and access http://ip:port/your project path/restuser/list. You get the JSON data.

Integrated SPRINGMVC

Integration into SPRINGMVC is not required for Serlvet initializers

Web. xml file Configuration

Integrate into SPRINGMVC, just use the SPRINGMVC servlet to

<servlet> <servlet-name>Spring</servlet-name> <servlet-class>org.springframework.web.ser Vlet. dispatcherservlet;</servlet-class> </servlet> <servlet-mapping> <servlet-name>spring</ Servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
Writing the Pojo class

Using the class annotations of the SPRINGMVC controller, you can integrate into the SPRINGMVC.

@Controller @path (Contactsresource.contacts_url) public class contactsresource{    public static final string contacts_url =  "/contacts";    @ autowired   contactservice service;    @GET     @Produces ({ Mediatype.application_xml, mediatype.application_json})     @Path ("Data")     Public contacts getall ()    {      return  Service.getall ();   }    @PUT     @POST     @Produces ({ Mediatype.application_xml, mediatype.application_json})     @Path ("Data")     Public response savecontact (@Context  uriinfo uri, contact contact)           throws URISyntaxException   {       service.save (contact);      uri newuri = uribuilder.fromuri (Uri.getPath ()). Path ( Contact.getlastname ()). build ();       return response.created (NewURI). Build ( );   }    @GET     @Produces ({mediatype.application_xml,  Mediatype.application_json})     @Path ("Data/{lastname}")    public contact  get (@PathParam ("LastName")  string lastname)    {       return service.getcontact (lastName);   }    @POST    @ put    @Consumes (mediatype.application_form_urlencoded)     @Produces ( mediatype.text_html)    public modelandview savecontactform (@Form  Contact  Contact)          throws URISyntaxException    {      service.Save (contact);       return viewall ();   }        @GET     @Produces (mediatype.text_html)    public  Modelandview viewall ()    {      // forward to  the  "Contacts"  view, with a request attribute named       //  "Contacts"  that has all of the existing contacts       return new modelandview ("Contacts",  "Contacts",  Service.getall ());    }}


Integration of SPRINGFRAMEWORK4 Series Resteasy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.