一,webservice的实现方式:
- JAX-WS,JAXM&SAA ,JAX-RS
WebService
-
天气预报:
1.写服务接口public interface IWeatherServer { public String queryWeather(String weather); }
2.在实现类上面添加注解 @WebService
@WebService public class WeatherServerImpl implements IWeatherServer { public String queryWeather(String city) { if(city.equals("河南")){ return city+":晴天"; }else{ return city+":冰雹"; }}}
3.发布服务:
Endpoint.publish("http://127.0.0.1:12345/weather?wsdl",new WeatherServerImpl());
4.生成客户端调用代码:
wsimport:wsimport -s . http://127.0.0.1:12345/weather?wsdl
5.客户端调用service:public static void main(String args[]){ WeatherServerImplService service=new WeatherServerImplService(); WeatherServerImpl port=service.getPort(WeatherServerImpl.class); String result=port.queryWeather("河南"); System.out.println(result); }
-
Soap协议有1.1和1.2两个版本:
-
默认发布1.1版本
-
发布1.2版本方法:@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
-
Soap1.1和Soap1.2的区别:
- 请求头content-tytpe:
1.1是text/xml;charset=utf-8
1.2是application/soap+xml;charset=utf-8 - 命名空间不同:
1.1:http://schemas.xmlsoap.org/soap/envelope
1.2:http://www.w3.org/2003/05/soap-envelope
- 请求头content-tytpe:
-
http的soap数据传输
URL url = new URL("http://127.0.0.1:12345/weather"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); connection.setDoOutput(true); OutputStream os = connection.getOutputStream(); os.write("河南".getBytes()); InputStream is = connection.getInputStream(); byte[] bys = new byte[1024]; int len = 0; while ((len = is.read(bys)) != -1) { System.out.println(new String(bys, 0, len, "utf-8")); } is.close(); os.close();
-
-
CXF发布WebService服务
-
导入cxf的jar包
-
编写服务接口:在接口上添加注解 @WebService
@WebService public interface IWeather { public String queryWeather(String city); public String[] getCityByProvince(String province); }
-
实现接口
-
发布服务
public static void main(String[] args) { //创建服务工厂Bean JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean(); //设置服务实现对象 factoryBean.setServiceBean(new WeatherImpl()); //设置服务发布地址 factoryBean.setAddress("http://127.0.0.1:12345/weather"); //创建服务 factoryBean.create(); }
-
生成客户端调用代码
- jdk1.8需要在jdk 的jre的lib目录下添加文件:jaxp.properties
javax.xml.accessExternalSchema=all - wsdl2java -d . http://127.0.0.1:12345/weather?wsdl
public static void main(String[] args) { JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean(); factoryBean.setServiceClass(IWeather.class); factoryBean.setAddress("http://127.0.0.1:12345/weather?wsdl"); IWeather weather=factoryBean.create(IWeather.class); System.out.println(weather.queryWeather("河南")); }
- jdk1.8需要在jdk 的jre的lib目录下添加文件:jaxp.properties
-
-
web-CXF-spring整合发布WebService服务:
-
导入cxf-spring 整合包
-
Spring配置文件applicationCotext.xml
<bean id="weatherService" class="com.xdd.WeatherImpl"></bean> <javaws:server address="/weather" serviceClass="com.xdd.WeatherImpl"> <javaws:serviceBean> <ref bean="weatherService"></ref> </javaws:serviceBean> </javaws:server> //另一种发布方式 <javaws:endpoint address="/hello" implementor="com.xdd.HelloImpl"></javaws:endpoint>
-
web.xml
//先配置读取spring配置文件 //配置servlet <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
-
生成客户端调用代码:
-
wsdl2java -d . http://localhost:8080/ws/weather?wsdl
-
在applicationContext.xml中配置
<javaws:client id="weatherService" serviceClass="com.xdd.IWeather" address="http://localhost:8080/ws/weather?wsdl"> </javaws:client>
-
调用服务:
public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IWeather weather= (IWeather) context.getBean("weatherService"); System.out.println(weather.getCityByProvince("河南")); }
-
-
-
CXF发布Rest服务:
-
创建接口:
@WebService @Path("student") public interface IStudentService { @GET @Path("/query/{id}") @Produces(MediaType.APPLICATION_XML) public Student query(@PathParam("id") int id); @GET @Path("/queryList/{name}") @Produces(MediaType.APPLICATION_XML) public List<Student> queryList(@PathParam("name") String name); }
-
创建实现类:
public class StudentServiceImpl implements IStudentService { @Override public Student query(int id) { Student student=new Student(id,"xdd","521314"); return student; } @Override public List<Student> queryList(String name) { Student student1=new Student(1,name,"521314"); List<Student> list=new ArrayList<>(); list.add(student1); return list; } }
-
发布服务:
public static void main(String[] args) { JAXRSServerFactoryBean jaxrsServerFactoryBean=new JAXRSServerFactoryBean(); jaxrsServerFactoryBean.setResourceClasses(StudentServiceImpl.class); jaxrsServerFactoryBean.setServiceBean(new StudentServiceImpl()); jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/user"); jaxrsServerFactoryBean.create(); }
-
测试:http://127.0.0.1:12345/user/student/queryList/1 (后面加上?_type=json可返回json格式 )
-
-
Jersey
UserServlet.java @Path("user") public class UserServlet { ObjectMapper mapper=new ObjectMapper(); @GET @Path("/list") @Produces(MediaType.APPLICATION_JSON) public String list() throws Exception { List<User> list=new ArrayList<User>(); list.add(new User(1,"xdd","521314")); return mapper.writeValueAsString(list); } }
配置web.xml
<servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.xdd.rest</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>