1.IDEA简历Maven模板webapp项目
2.Pom文件添加依赖项
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rminfo</groupId> <artifactId>test_RestWebservice</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>test_RestWebservice Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.1.4.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- web项目编译时需要,部署到tomcat中就不需要了 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>2.4.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.13</version> </dependency> </dependencies> <build> <finalName>test_RestWebservice</finalName> </build> </project>
3. 修改web.xml文件
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/transport-servlet.xml</param-value> </context-param> <servlet> <servlet-name>transport</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>transport</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/userService/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
4. 添加Spring配置文件transport-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd "> <mvc:annotation-driven/> <context:component-scan base-package="com.rminfo.controller"/> <jaxrs:server id="userRestService" address="/userservices"> <jaxrs:serviceBeans> <ref bean="userServiceImpl"/> </jaxrs:serviceBeans> </jaxrs:server> <bean id="userServiceImpl" class="com.rminfo.controller.UserServiceImpl"></bean> </beans>
5. 建立实体类User,添加注解@XmlRootElement(name = "User")
否则会出错:No message body writer has been found for response class User.
package com.rminfo.controller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; /** * Created by carl on 2016/6/23. */ @XmlRootElement(name = "User") public class User { private String userId; private String nickname; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } @Override public String toString() { return "User{" + "userId=‘" + userId + ‘\‘‘ + ", nickname=‘" + nickname + ‘\‘‘ + ‘}‘; } }
6. WebService接口类添加注解,以及其实现
package com.rminfo.controller; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; /** * Created by carl on 2016/6/23. */ public interface UserService { @GET @Produces(MediaType.APPLICATION_JSON) @Path("/searchUser/{id}") public User searchUser(@PathParam("id") String userId); }
7. 测试是否发布成功:http://localhost:8080/test_RestWebservice/userService
出现上述画面说明发布成功。
时间: 2024-10-02 01:04:28