Jax-rs Development (ii): Hello World

Source: Internet
Author: User

1. Establish Maven WebApp Engineering aty-rest.

2. Add spring Framework, JAX-RS interface, CXF implementation in POM file

<dependency><groupid>javax.ws.rs</groupid><artifactid>javax.ws.rs-api</artifactid ><version>2.0</version></dependency><dependency><groupid>org.springframework </groupId><artifactId>spring-web</artifactId><version>3.1.1.RELEASE</version> </dependency><dependency><groupId>org.apache.cxf</groupId><artifactId> Cxf-rt-frontend-jaxrs</artifactid><version>3.0.0</version></dependency>

3. Writing the rest interface and implementing classes

public interface inameservice{    @GET    @Path ("/welcome/")    @Produces (Mediatype.application_json)    Public String Welcome ();} @Component ("Nameserviceimpl") public class Nameserviceimpl implements inameservice{public    String Welcome ()    {        return "{\" name\ ": 123}";}    }

Start sping and CXF in 4.web.xml

<!--Spring--><context-param><param-name>contextconfiglocation</param-name>< param-value>classpath:spring.xml</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><!--CXF--><servlet><servlet-name>cxf</servlet-name><servlet-class> org.apache.cxf.transport.servlet.cxfservlet</servlet-class></servlet><servlet-mapping>< Servlet-name>cxf</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping>

4. Configure the cxf-spring.xml and include it in the Spring.xml

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs= "Http://cxf.apache.org/jaxrs" xsi: Schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd "><jaxrs:server address= "/greet" ><jaxrs:servicebeans><ref bean= "Nameserviceimpl"/></jaxrs:servicebeans> </jaxrs:server></beans></span>


5. Use Maven to package and deploy the war to Tomcat.

Everything is normal and you can access our rest services via Http://127.0.0.1:8080/aty-rest/rest/greet/welcome.





Jax-rs Development (ii): Hello World

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.