WebService系列2 CarService

本文演示webservice的参数传递。
鉴于webservice对Map类型的参数和返回值的支持非常的差,如果要做需要些各种mapper进行转换,费心费力,我们是否可以考虑通过传递json字符串参数和返回json字符串实现数据传输呢?

注意:本文用Fastjson.jar实现json字符的序列化和反序列化,支持国产开源。 :D

1、实体类

package com.ws.jaxws.car.entity;

public class Car{
private int id;
private String brand;
private double price;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}


public Car(){

}

public Car(int id, String brand, double price) {
super();
this.id = id;
this.brand = brand;
this.price = price;
}

@Override
public String toString() {
return "Car [ id:" + this.id + ",brand:" + this.brand + ",price:" + this.price +"]";
}
}


2、接口

package com.ws.jaxws.car.service;


import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import com.ws.jaxws.car.entity.Car;

@WebService
public interface CarService {
@WebMethod
public String getPrice(@WebParam String brand);

@WebMethod
public List<Integer> getCarList(@WebParam List<String> strs);//演示参数传递,传入List<String>类型参数,返回List<Integer>参数

@WebMethod
public List<Car> getCars();//演示返回List<Car>类型的值

public String getCarsAsJson(String json);//Webservice对Map类型的参数和返回值不支持,需要写一些复杂的转换器,换一种思路通过json传递参数呗~
}


3、实现类

package com.ws.jaxws.car.service.impl;

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

import javax.jws.WebService;

import com.alibaba.fastjson.JSON;
import com.ws.jaxws.car.entity.Car;
import com.ws.jaxws.car.service.CarService;

@WebService(endpointInterface = "com.ws.jaxws.car.service.CarService", serviceName = "carWs")
public class CarServiceImpl implements CarService {
private static Map<String, String> price = new HashMap<String, String>();
static {
price.put("BMW", "$100,000");
price.put("BENZ", "$500,000");
price.put("FORD", "$235,500");
price.put("ROLLS-ROYCE", "$5000,000");
}

@Override
public String getPrice(String brand) {
return price.get(brand);
}

@Override
public List<Integer> getCarList(List<String> strs) {
List<Integer> list = new ArrayList<Integer>();
for (String str : strs) {
list.add(Integer.parseInt(str));
}
return list;
}

@Override
public List<Car> getCars() {
List<Car> cars = new ArrayList<Car>();
for (int i = 0; i < 5; i++) {
cars.add(new Car(i, "car" + i, 100000.00));
}
return cars;
}

@Override
public String getCarsAsJson(String jsonparam) {

//有了json字符串,想怎么转就怎么转,哈哈
Map<String, Object> map = (Map<String, Object>) JSON.parse(jsonparam);
Car car = JSON.parseObject(jsonparam, Car.class);

System.out.println("map:" + map);
System.out.println("car:" + car);

List<Car> carList = new ArrayList<Car>();
for (int i = 0; i < 5; i++) {
carList.add(new Car(i, "brand" + i, i));
}
System.out.println("json:" + JSON.toJSONString(carList, false));
return JSON.toJSONString(carList);
}
}


5、服务端

package com.ws.jaxws.car.server;


import javax.xml.ws.Endpoint;

import com.ws.jaxws.car.service.impl.CarServiceImpl;


public class CarServer {
public static void main(String[] args) throws InterruptedException {
Endpoint.publish("http://localhost:8080/carWs", new CarServiceImpl());

System.out.println("FooServer is ready....go to your browser and type: http://localhost:8080/carWs?wsdl");

Thread.sleep(5 * 60 * 1000);
System.out.println("Server exiting");
System.exit(0);
}
}


6、客户端

package com.ws.jaxws.car.client;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.alibaba.fastjson.JSON;
import com.ws.jaxws.car.entity.Car;
import com.ws.jaxws.car.service.CarService;

public class CarServiceClient {
public static void main(String[] args) throws MalformedURLException {

String url = "http://localhost:8080/carWs?wsdl";
String targetNamespace = "http://impl.service.car.jaxws.ws.com/";
String name = "carWs";
Service service = Service.create(new URL(url), new QName(targetNamespace, name));
CarService carService = service.getPort(CarService.class);


String price = carService.getPrice("BMW");
System.out.println("price:" + price);
System.out.println("=============================");



List<String> list = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
list.add(i+"");
}
List idList = carService.getCarList(list);
System.out.println("idList:" + idList);
System.out.println("=============================");

List<Car> carList = carService.getCars();
System.out.println("carList:" + carList);
System.out.println("=============================");


Map paramMap = new HashMap();
paramMap.put("id", 3306);
paramMap.put("brand", "BMW");
paramMap.put("price", 100000.00);
String str = carService.getCarsAsJson(JSON.toJSONString(paramMap));
System.out.println("str:" + str);

List<Car> cars = (List<Car>)JSON.parseArray(str, Car.class);
for(Car c:cars){
System.out.println(c);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值