我正在使用Apache CXF的示例REST服务,但不知怎的,我无法调用该服务。我的实现类是,
package com.ananth.lab.cfxrest.service;
import com.ananth.lab.cfxrest.vo.Address;
import com.ananth.lab.cfxrest.vo.Employee;
import org.springframework.stereotype.Service;
import javax.jws.WebService;
import javax.ws.rs.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Path("/cservice")
@Produces("application/xml")
public class EmployeeService {
@GET
@Path("/emp")
public Employee getEmployee() {
Address address1 = new Address();
address1.setCity("Chennai");
address1.setZip(63);
List<Address> list = new ArrayList<Address>();
Address address2 = new Address();
address2.setCity("Bangalore");
address2.setZip(49);
list.add(address1);
list.add(address2);
Employee emp = new Employee();
emp.setAddress(list);
emp.setEmployeeId("001");
emp.setEmployeeName("Ananth");
return emp;
}
}
我的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>Hello world REST service with apache cxf</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml,WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
我部署在Tomcat中,上下文路径是&#34; Lab&#34;。所以我试图访问像
这样的服务http://localhost:8080/Lab/cservice/emp
我正在
No service was found.
答案 0 :(得分:1)
我认为你应该确保你的Employee类有正确的“@”评论。如下所示:
@XmlRootElement(name="cservice")
public class Employee (){
private String employeeId;
private String employeeName;
...
@XmlElement(name="employeeId")
public void setEmployeeId(String employeeId){
...
}
public String getEmployeeId(){
return this.employeeId;
}
@XmlElement(name="employeeName")
public void setEmployeeName(String employeeId){
...
}
public String getEmployeeName(){
return this.employeeName
}
...
...
}
我建议您查看WEB-INF / beans.xml&amp; WEB-INF / applicationContext.xml中。
答案 1 :(得分:-1)
确保您的请求网址端点与您的服务器端点匹配。
将webl和beans.xml中的url结合起来产生终结点