java restful

JAVA服务端RESTFUL:

http://localhost:8080/CxfRest/rest/userinfo/bean/get/3/json

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        QName svc_name 

        QName svc_name  =      new  QName( " http://10.56.28.34:977/ebnmstest/soap/dispatch " " dispatch " );
        
        factory.setServiceClass(dispatch.
class );
        factory.setAddress(
" http://10.56.28.34:977/ebnmstest/soap/dispatch " );
        factory.setServiceName (svc_name);
        logger.info (
" 2 " );
        
// factory.getServiceFactory().setDataBinding(new AegisDatabinding());
        dispatch client  =  (dispatch) factory.create();
        logger.info (
" 3 " );

        AddHostData m 
=   new  AddHostData ();
        m.name 
=   " good " ;
        m.otherValues 
=   new  HashMap ();
        m.otherValues.put (
" hehe " " haha " );     
        m.otherValues.put (
" hehe1 " " haha1 " );     
        m.otherValues.put (
" hehe2 " " haha2 " );     

        System.out.println (client.hello (m));


RESTFUL:

1、webxml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:context-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-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>

2、application-Context.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:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.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="UserInfo" class="com.cxfs.ws.restful.sample.service.RESTSampleSource" />
<!-- 这里的地址很重要,客户端需要通过这个地址来访问WebService -->
<jaxrs:server id="restServiceContainer" address="/rest">
<jaxrs:serviceBeans>
<ref bean="UserInfo" />
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
<entry key="en" value="en-gb" />
</jaxrs:languageMappings>
</jaxrs:server>
</beans>

3、RESTFUL code:

package com.cxfs.ws.restful.sample.service;


import java.io.IOException;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;


import com.cxfs.ws.restfu.sample.entity.MapBean;
import com.cxfs.ws.restfu.sample.entity.User;
import com.cxfs.ws.restfu.sample.entity.Users;


@Path("/userinfo")
public interface RESTSample {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String doGet();


@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/request/{param}")
public String doRequest(@PathParam("param") String param,
@Context HttpServletRequest servletRequest,
@Context HttpServletResponse servletResponse);


@GET
@Path("/bean/get/{id}/{format}")
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User getBean(@PathParam("id") int id,@PathParam("format")String format);


@GET
@Path("/list/get/{format}")
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Users getList(@PathParam("format")String format);


@GET
@Path("/map/get")
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public MapBean getMap();

@POST
    @Path("/postData/add/{format}")
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public User postData(User user,@PathParam("format")String format) throws IOException;

@PUT
    @Path("/putData/update/{id}")
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public User putData(@PathParam("id") int id, User user);

@DELETE
    @Path("/removeData/delete/{id}")
    public void deleteData(@PathParam("id") int id);
}



package com.cxfs.ws.restful.sample.service;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;


import com.cxfs.ws.restfu.sample.entity.MapBean;
import com.cxfs.ws.restfu.sample.entity.User;
import com.cxfs.ws.restfu.sample.entity.Users;
@Path("/userinfo")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public class RESTSampleSource implements RESTSample {
@Context
private UriInfo uriInfo;
@Context
private Request request;
@DELETE
@Path("/removeData/{id}")
public void deleteData(@PathParam("id") int id) {
System.out.println("#######deleteData#######" + id);
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String doGet() {
return "this is get rest request";
}
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/request/{param}")

public String doRequest(@PathParam("param")String param, HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {


return "success";
}
@GET
@Path("/bean/get/{id}/{format}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User getBean(@PathParam("id") int id,@PathParam("format")String format) {


System.out.println("####getBean#####");
System.out.println("id:" + id);
System.out.println("Method:" + request.getMethod());
System.out.println("uri:" + uriInfo.getPath());
System.out.println(uriInfo.getPathParameters());
User user = new User();
user.setId(id);
user.setName("JojO");
Response.ResponseBuilder
return user;
}
@GET
@Path("/map/get")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public MapBean getMap() {
Map<String,User> map = new HashMap<String,User>();
User user = null;
for(int i=0;i<4;i++){
user= new User();
user.setId(i);
user.setName("JojO-" + i);
map.put("key-"+i, user);
}
MapBean bean = new MapBean();
bean.setMap(map);
return bean;
}
@POST
    @Path("/postData/add/{format}")
    @Consumes({ MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON })
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public User postData(User user,@PathParam("format")String format) throws IOException {
System.out.println(user);
user.setName("jojo##12321321");
return user;
}
//更新某一方法
@PUT
@Path("/putData/update/{id}")
@Consumes({ MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces( { MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })
public User putData(@PathParam("id")int id, User user) {


System.out.println("#####putData#####");
System.out.println(user);
user.setId(id);
user.setAddress("hoojo#gz");
user.setEmail("hoojo_@126.com");
user.setName("hoojo");
System.out.println(user);
return user;
}
@GET
@Path("/list/get/{format}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Users getList(@PathParam("format")String format) {
System.out.println("####getList#####");
System.out.println("Method:" + request.getMethod());
System.out.println("uri:" + uriInfo.getPath());
System.out.println(uriInfo.getPathParameters());
List<User> list = new ArrayList<User>();
User user = null;
for (int i = 0; i < 4;i ++) {
user = new User();
user.setId(i);
user.setName("JojO-" + i);
list.add(user);
}
Users users = new Users();
users.setUsers(list);
return users;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值