1.IDEA简历Maven模板webapp项目
2.Pom文件添加依赖项
4.0.0 com.rminfo test_RestWebservice war 1.0-SNAPSHOT test_RestWebservice Maven Webapp http://maven.apache.org 4.1.4.RELEASE junit junit 3.8.1 test org.springframework spring-core ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-webmvc ${spring.version} javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.2 provided com.fasterxml.jackson.core jackson-databind 2.3.4 com.fasterxml.jackson.core jackson-annotations 2.4.0 com.fasterxml.jackson.core jackson-core 2.4.3 javax.ws.rs jsr311-api 1.1.1 org.apache.cxf cxf-rt-frontend-jaxrs 2.4.4 org.codehaus.jackson jackson-jaxrs 1.9.13 test_RestWebservice
3. 修改web.xml文件
Archetype Created Web Application contextConfigLocation /WEB-INF/transport-servlet.xml transport org.springframework.web.servlet.DispatcherServlet 1 transport / CXFServlet org.apache.cxf.transport.servlet.CXFServlet 2 CXFServlet /userService/* org.springframework.web.context.ContextLoaderListener
4. 添加Spring配置文件transport-servlet.xml
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
出现上述画面说明发布成功。