webservice的发布和调用及使用fastJson时列的排序

时间:2021-10-15 00:52:14

我这里是要发布一个json所以建了几个bean。首先,建立服务端,最简单的new一个web项目。

webservice的发布和调用及使用fastJson时列的排序

建立完后开始创建bean类,首先是User.java

package com.fhit.webservices.pojo;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

public class User {
	 @JSONField(ordinal = 1)  
	private int txtmlid;
	 @JSONField(ordinal = 2)  
	private String mlh;
	 @JSONField(ordinal = 3)  
	private String failing_people;
	 @JSONField(ordinal = 4)  
	private String flrq;
	 @JSONField(ordinal = 5)  
	private String all_sldtxt;
	 @JSONField(ordinal = 6)  
	private String ml_mingcheng;
	 @JSONField(ordinal = 7)  
	private List<itemList> itemList;
	
	public User() {	}

	public User(int txtmlid, String mlh, String failing_people, String flrq, String all_sldtxt, String ml_mingcheng,
			List<itemList> itemList) {
		this.txtmlid = txtmlid;
		this.mlh = mlh;
		this.failing_people = failing_people;
		this.flrq = flrq;
		this.all_sldtxt = all_sldtxt;
		this.ml_mingcheng = ml_mingcheng;
		this.itemList = itemList;
	}

	public int getTxtmlid() {
		return txtmlid;
	}

	public void setTxtmlid(int txtmlid) {
		this.txtmlid = txtmlid;
	}

	public String getMlh() {
		return mlh;
	}

	public void setMlh(String mlh) {
		this.mlh = mlh;
	}

	public String getFailing_people() {
		return failing_people;
	}

	public void setFailing_people(String failing_people) {
		this.failing_people = failing_people;
	}

	public String getFlrq() {
		return flrq;
	}

	public void setFlrq(String flrq) {
		this.flrq = flrq;
	}

	public String getAll_sldtxt() {
		return all_sldtxt;
	}

	public void setAll_sldtxt(String all_sldtxt) {
		this.all_sldtxt = all_sldtxt;
	}

	public String getMl_mingcheng() {
		return ml_mingcheng;
	}

	public void setMl_mingcheng(String ml_mingcheng) {
		this.ml_mingcheng = ml_mingcheng;
	}

	public List<itemList> getItemList() {
		return itemList;
	}

	public void setItemList(List<itemList> itemList) {
		this.itemList = itemList;
	}
}

然后是itemList.java

package com.fhit.webservices.pojo;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

public class itemList {
	@JSONField(ordinal = 1) 
	private String mlh;
	@JSONField(ordinal = 2) 
	private String flrq;
	@JSONField(ordinal = 3) 
	private String mlnr;
	@JSONField(ordinal = 4) 
	private String pinlei;
	@JSONField(ordinal = 5) 
	private String xianghao;
	@JSONField(ordinal = 6) 
	private String yuanjing;
	@JSONField(ordinal = 7) 
	private String gaijing;
	@JSONField(ordinal = 8) 
	private String daoju;
	@JSONField(ordinal = 9) 
	private String daozhan;
	@JSONField(ordinal = 10) 
	private List<item3List> item3List;
	
	public itemList(){}

	public itemList(String mlh, String flrq, String mlnr, String pinlei, String xianghao, String yuanjing,
			String gaijing, String daoju, String daozhan, List<item3List> item3List) {
		this.mlh = mlh;
		this.flrq = flrq;
		this.mlnr = mlnr;
		this.pinlei = pinlei;
		this.xianghao = xianghao;
		this.yuanjing = yuanjing;
		this.gaijing = gaijing;
		this.daoju = daoju;
		this.daozhan = daozhan;
		this.item3List = item3List;
	}

	public String getMlh() {
		return mlh;
	}

	public void setMlh(String mlh) {
		this.mlh = mlh;
	}

	public String getFlrq() {
		return flrq;
	}

	public void setFlrq(String flrq) {
		this.flrq = flrq;
	}

	public String getMlnr() {
		return mlnr;
	}

	public void setMlnr(String mlnr) {
		this.mlnr = mlnr;
	}

	public String getPinlei() {
		return pinlei;
	}

	public void setPinlei(String pinlei) {
		this.pinlei = pinlei;
	}

	public String getXianghao() {
		return xianghao;
	}

	public void setXianghao(String xianghao) {
		this.xianghao = xianghao;
	}

	public String getYuanjing() {
		return yuanjing;
	}

	public void setYuanjing(String yuanjing) {
		this.yuanjing = yuanjing;
	}

	public String getGaijing() {
		return gaijing;
	}

	public void setGaijing(String gaijing) {
		this.gaijing = gaijing;
	}

	public String getDaoju() {
		return daoju;
	}

	public void setDaoju(String daoju) {
		this.daoju = daoju;
	}

	public String getDaozhan() {
		return daozhan;
	}

	public void setDaozhan(String daozhan) {
		this.daozhan = daozhan;
	}

	public List<item3List> getItem3List() {
		return item3List;
	}

	public void setItem3List(List<item3List> item3List) {
		this.item3List = item3List;
	}
}

然后是item3List.java

package com.fhit.webservices.pojo;

import com.alibaba.fastjson.annotation.JSONField;

public class item3List {
	@JSONField(ordinal = 1) 
	private String chehao;
	@JSONField(ordinal = 2) 
	private String checi;
	@JSONField(ordinal = 3) 
	private String liangshu;
	public item3List() {	}
	public item3List(String chehao, String checi, String liangshu) {
		this.chehao = chehao;
		this.checi = checi;
		this.liangshu = liangshu;
	}
	public String getChehao() {
		return chehao;
	}
	public void setChehao(String chehao) {
		this.chehao = chehao;
	}
	public String getCheci() {
		return checi;
	}
	public void setCheci(String checi) {
		this.checi = checi;
	}
	public String getLiangshu() {
		return liangshu;
	}
	public void setLiangshu(String liangshu) {
		this.liangshu = liangshu;
	}
}

然后是服务端main的建立Function.java注意添加@WebService

package com.fhit.webservices;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fhit.webservices.pojo.User;
import com.fhit.webservices.pojo.item3List;
import com.fhit.webservices.pojo.itemList;


	@WebService
	public class Function {
		
	public String getMLData(){
		return "20180101";
	}
		
	public String OutJosn(){
		List<Map<String, User>> l=new ArrayList<>();
		Map<String,User> map=new HashMap<>();
		List<itemList> iList=new ArrayList<>();
		List<item3List> i3List=new ArrayList<>();
		l.add(map);
		
		User u=new User();
		u.setTxtmlid(797532);
		u.setMlh("4401");
		u.setFailing_people("武鑫");
		u.setFlrq(new SimpleDateFormat("yyyy-MM-dd hh:MM:ss").format(new Date()));
		u.setAll_sldtxt("B;T;P;F;Y;K;Q;Z;J;O");
		u.setMl_mingcheng("行调迂回命令");
		u.setItemList(iList);
		map.put(u.getMlh(), u);
		
		itemList it=new itemList();
		it.setMlh("4401");
		it.setFlrq(u.getFlrq());
		it.setMlnr("因能力紧张,准哈局82008次66辆原经阿尔山北到沈局穆家店车流改经泰来迂回运输。");
		it.setPinlei("焦炭");
		it.setXianghao("13");
		it.setYuanjing("阿尔山北");
		it.setGaijing("泰来");
		it.setDaoju("沈局");
		it.setDaozhan("穆家店");
		it.setItem3List(i3List);
		iList.add(it);
		
		item3List it3=new item3List();
		it3.setChehao("4960020|5222812|5250396|3315504|3828431|4910826|4869665|1584734|4917441|4939375|3109466|3463507|3313169|3463320|3101119|3411576|3826239|1619456|1625576|1654412|1630142|1658716|1723475|4876998|4897949|4911697|1553187|1634608|1596426|1678896|4969490|1614181");
		it3.setCheci("28002");
		it3.setLiangshu("32");
		i3List.add(it3);
		
		return JSON.toJSONString(l,SerializerFeature.DisableCircularReferenceDetect);
     }
     
     public static void main(String[] args) {
         // 使用Endpoint(终端)类发布webservice
         Endpoint.publish("http://192.168.31.82:8089/service/function", new Function());
         System.out.println("Publish Success");
     }
 }

然后右键执行该程序访问http://192.168.31.82:8089/service/function?wsdl看有没有出现类似的xml有则成功了。

下面是客户端的调用

首先还是先new一个web项目,然后在项目名上new other一个web service client

webservice的发布和调用及使用fastJson时列的排序

下一步输入http://192.168.31.82:8089/service/function?wsdl

webservice的发布和调用及使用fastJson时列的排序

然后finish后会出现几个系统生成的文件

webservice的发布和调用及使用fastJson时列的排序

然后你可以建立一个test程序测试一下

package com.fhit.webservices;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

public class Test {
	public static void main(String[] args) throws ServiceException, RemoteException {

		Function f=new FunctionServiceLocator().getFunctionPort();
		System.out.println(f.outJosn());
		System.out.println(f.getMLData());
	}
}

然后控制台输出如下

webservice的发布和调用及使用fastJson时列的排序

我这里主要是想要一下这个json数据

[{
	"4401": {
		"txtmlid": 797532,
		"mlh": "4401",
		"failing_people": "武鑫",
		"flrq": "2018-06-08 02:06:40",
		"all_sldtxt": "B;T;P;F;Y;K;Q;Z;J;O",
		"ml_mingcheng": "行调迂回命令",
		"itemList": [{
			"mlh": "4401",
			"flrq": "2018-06-08 02:06:40",
			"mlnr": "因能力紧张,准哈局82008次66辆原经阿尔山北到沈局穆家店车流改经泰来迂回运输。",
			"pinlei": "焦炭",
			"xianghao": "13",
			"yuanjing": "阿尔山北",
			"gaijing": "泰来",
			"daoju": "沈局",
			"daozhan": "穆家店",
			"item3List": [{
				"chehao": "4960020|5222812|5250396|3315504|3828431|4910826|4869665|1584734|4917441|4939375|3109466|3463507|3313169|3463320|3101119|3411576|3826239|1619456|1625576|1654412|1630142|1658716|1723475|4876998|4897949|4911697|1553187|1634608|1596426|1678896|4969490|1614181",
				"checi": "28002",
				"liangshu": "32"
			}]
		}]
	}
}]
这里就已经完成了对webservices的调用