cxf rest

<?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"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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" />
<!--
需要说明的而是,JsonProvider的属性 dropRootElement 默认为false,则Json格式会将类名作为第一个节点,如
{Customer:{"id":123,"name":"John"}},如果配置为true,则Json格式为{"id":123,"name":"John"},
而Gson等Json包会解析格式为后者。

而dropCollectionWrapperElement属性默认为false,则当遇到Collection时,Json会在集合中将容器中类名作为一个节点,
比如{"Customer":{{"id":123,"name":"John"}}},而设置为false,则JSon格式为{{"id":123,"name":"John"}}

而serializeAsArray属性默认为false,则当遇到Collecion时,格式为{{"id":123,"name":"John"}},如果设置为true,
则格式为[{"id":123,"name":"john"}],而Gson等解析为后者。
-->
<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
<property name="dropRootElement" value="true"/>
<property name="dropCollectionWrapperElement" value="true"/>
<property name="serializeAsArray" value="true"/>
</bean>

<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="roomService"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
<bean id="roomService" class="com.platform.restful.demo.RoomService" />
<jaxws:endpoint id="helloWorld" implementor="com.platform.webservice.demo.HelloWorldImpl" address="/HelloWorld" />
</beans>

 

 

package com.platform.restful.demo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

 

@Path("/roomservice")
@Produces("application/json")
public class RoomService {

@GET
@Path("/room/{id}")
@Consumes("application/xml")
public Room getRoom(@PathParam("id")String id )
{
System.out.println("get room by id= "+id);
Room room=RoomDAO.getRoom(id);
return room;
}
@GET
@Path("/room")
@Consumes("application/xml")
public Rooms getAllRoom()
{
System.out.println("get all room");
Rooms rooms=RoomDAO.getRooms();
return rooms;
}

@GET
@Path("/person/{id}")
@Consumes("application/xml")
public Person findPerson(@PathParam("id") String id){
return RoomDAO.findPersonById(id);
}

@GET
@Path("/person")
@Consumes("application/xml")
public List<Person> findAllPerson(){
return RoomDAO.findAllPerson();
}

@POST
@Path("/room")
@Consumes("application/xml")
public boolean addRoom(String roomId)
{
System.out.println("add room which id is:"+roomId);
//RoomDAO.addRoom(room);
System.out.println();
return true;
}
@PUT
@Path("/room/{id}")
@Consumes("application/xml")
public void updateRoom(@PathParam("id")String id,Room room)
{
System.out.println("update room which original id is:"+room.getId());
RoomDAO.updateRoom(id,room);
}
@DELETE
@Path("/room/{id}")
@Consumes("application/xml")
public void deleteRoom(@PathParam("id")String id)
{
System.out.println("remove room by id= "+id);
RoomDAO.deleteRoom(id);
}
@POST
@Path("/room/{id}")
@Consumes("application/xml")
public void addPerson(@PathParam("id") String id,Person person)
{
System.out.println("add person who's name is:"+person.getName());
RoomDAO.addPerson(id, person);
}
@DELETE
@Path("/room/{id}/{name}")
@Consumes("application/xml")
public void deletePerson(@PathParam("id")String id,@PathParam("name")String name)
{
System.out.println("remove person who's name is: "+name);
RoomDAO.deletePerson(id, name);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值