我现在有个问题,整了很长时间了,还没整出来。。
直接上代码:
POJO类:
@XmlRootElement(name = "UserVO")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserVO {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DAO类:
public class UserDAO {
private static Map<String, UserVO> userVOs;
static {
userVOs = new HashMap<String, UserVO>();
UserVO u1 = new UserVO();
u1.setId(1);
u1.setName("zjp");
UserVO u2 = new UserVO();
u2.setId(2);
u2.setName("zzzzz");
userVOs.put("1", u1);
userVOs.put("2", u2);
}
public static Map<String, UserVO> getPersons() {
return userVOs;
}
public static UserVO getPerson() {
return userVOs.get("1");
}
}
Service类:
@Path("/userservice")
public class UserService {
@GET
@Path("/getUsers")
@Produces({"application/json","application/xml"})
public UserVOMap getUsers() {
System.out.println("get users ");
Map<String, UserVO> userVOs = UserDAO.getPersons();
UserVOMap userVOMap = new UserVOMap();
userVOMap.setUserVOs(userVOs);
return userVOMap;
}
@GET
@Path("/getUser")
@Produces("application/json")
public UserVO getUser() {
System.out.println("get user ");
UserVO userVO = UserDAO.getPerson();
return userVO;
}
}
Spring的配置文件:
<?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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />-->
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="userService" class="user.UserService">
</bean>
<!-- 配置需要暴露的BeanService -->
<jaxrs:server id="restContainer" address="/user/" >
<jaxrs:serviceBeans>
<ref bean="userService" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
</jaxrs:server>
</beans>
服务器是用Jetty发布的,现在访问:http://localhost/user/userservice/getUser想得到JSON格式的数据,可一直失败,响应XML格式的正常。。
请大神们帮忙看看。。是不是哪里配置出错了。。。
386 阅读