标签:out name app http vax nbsp public int print
1、新建接口类,并声明为webService
package com.glaway.ws.webservice; import javax.jws.WebService; @WebService public class WsTest2 { }
2、在类中新建接口方法,返回值都是String,是通过Json工具类将结果对象都封装成json字符串
package com.glaway.ws.webservice; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import com.glaway.ws.bean.User; import com.glaway.ws.util.JsonUtils; @WebService public class WsTest2 { //对象,封装成json字符串,否则报错 public String getUser() { User user = new User(); user.setName("科比"); user.setAge(40); String json = JsonUtils.bean2Json(user); return json; } //Map,转换成json字符串 public String getMap(String name,int age) { Map<String,String> map = new HashMap<String,String>(); map.put("name", name); map.put("age", age+""); map.put("mes", "你的姓名是"+name+",年龄是"+age); String json = JsonUtils.map2Json(map); return json; } //List,转换成json public String listTest() { List<String> list = new ArrayList<String>(); list.add("我"); list.add("是"); list.add("中"); list.add("国"); list.add("人"); return JsonUtils.list2Json(list); } //List中存放map,转换成json public String listMapTest() { List<Map<String,String>> list = new ArrayList<Map<String,String>>(); Map<String,String> map1 = new HashMap<String,String>(); map1.put("a", "a1"); map1.put("b", "b1"); list.add(map1); Map<String,String> map2 = new HashMap<String,String>(); map2.put("我", "我是谁"); list.add(map2); return JsonUtils.list2Json(list); } //list中存放bean,转换成json public String listBeanTest() { User u1 = new User("1",25); User u2 = new User("2",8); List<User> list = new ArrayList<User>(); list.add(u1); list.add(u2); Map map = new HashMap(); map.put("code", "success"); map.put("data", list); System.out.println("返回结果->"+JsonUtils.map2Json(map)); return JsonUtils.map2Json(map); } }
3、启动webService
package com.glaway.ws; import javax.xml.ws.Endpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.glaway.ws.webservice.WsTest1; import com.glaway.ws.webservice.WsTest2; /** * Hello world! */ @SpringBootApplication public class WebApplication { public static void main( String[] args ) { SpringApplication.run(WebApplication.class, args); /** * Endpoint.publish(url,service) * ip是发布接口的项目所在的ip * port:不可与发布接口的项目所在端口冲突,另起一个端口 * name:随便起 * * 1、端口号可以重复,也可以不重复,只要不与tomcat端口冲突即可 * 2、ip可以是localhost、127.0.0.1、192.168.11.136,它的作用只是找到接口服务所在的电脑 * 3、接口的放回类型只能是基本类型,如果想返回对象类型,比如Bean,Map,List等,则需要将对象转换成json然后返回字符串 */ //发布接口:基本类型数据 Endpoint.publish("http://localhost:8080/test1", new WsTest1()); //发布接口:实体类 Endpoint.publish("http://localhost:8080/test2", new WsTest2()); } }
4、启动成功后,浏览器访问wsdl地址,结果如下
标签:out name app http vax nbsp public int print
原文地址:https://www.cnblogs.com/wf2010517141/p/10852309.html