SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

您所在的位置:网站首页 dao层怎么写 SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

2023-03-21 01:44| 来源: 网络整理| 查看: 265

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis 思创斯忠实用户-ss • 2023年3月19日 14:51 • 未分类

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis1、本demo目的查询学生信息【为了方便没有写批量查询,也就是说以下getAllStudents也是一个一个查询【而且没有测试该方法正确性,之所以写只是为了规范,返回Student的集合Students也是一个独立的实体,非必需,请忽略】2、主要是网上好多不全,整合的时候各种坑,我就记录一下。一、老规矩,先搞个数据库命名demo【可自己修改】/*NavicatMySQLDataTransf…

大家好,我是你的好朋友思创斯。今天说一说SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis,希望您对编程的造诣更进一步.

 

1、本demo目的查询学生信息【为了方便没有写批量查询,也就是说以下getAllStudents也是一个一个查询【而且没有测试该方法正确性,之所以写只是为了规范,返回Student的集合Students也是一个独立的实体,非必需,请忽略】

2、主要是网上好多不全,整合的时候各种坑,我就记录一下。

一、老规矩,先搞个数据库 命名demo【可自己修改】 /* Navicat MySQL Data Transfer Source Server : root Source Server Version : 50540 Source Host : localhost:3306 Source Database : demo Target Server Type : MYSQL Target Server Version : 50540 File Encoding : 65001 Date: 2018-03-23 16:21:20 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `student` -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(10) NOT NULL, `name` varchar(100) DEFAULT NULL, `sex` char(6) DEFAULT NULL, `address` varchar(255) DEFAULT NULL, `age` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', '李斯', '男', '江西南昌', '22');

ps【忽略细节,只是为了说明问题】

二、maven依赖【可能不是最少依赖】 4.0.0 cn.edu.jxnu demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-jersey org.springframework.boot spring-boot-starter-web org.apache.cxf cxf-spring-boot-starter-jaxrs 3.1.11 org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test com.alibaba fastjson 1.2.7 org.jsoup jsoup 1.9.2 org.apache.cxf cxf-rt-frontend-jaxws 3.1.6 org.apache.cxf cxf-rt-transports-http 3.1.6 org.apache.cxf cxf-rt-transports-http-jetty 3.1.6 org.springframework.boot spring-boot-maven-plugin

 

三、实体类 package cn.edu.jxnu.entity; import java.io.Serializable; import javax.xml.bind.annotation.XmlRootElement; /** * 学生实体类 * * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:42:04 * @version: V1.0 * */ @XmlRootElement(name = "Student") public class Student implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private char sex; private String address; private Integer age; public Student() { super(); } public Student(Integer id, String name, char sex, String address, Integer age) { super(); this.id = id; this.name = name; this.sex = sex; this.address = address; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", address=" + address + ", age=" + age + "]"; } }

学生集合实体

  package cn.edu.jxnu.entity; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * 封装多个学生实体类 * * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:42:14 * @version: V1.0 * */ @XmlRootElement(name = "Students") public class Students { private List students; public Students(List students) { super(); this.students = students; } @XmlElement(name = "Student") public List getStudents() { return students; } public Students() { super(); } public void setStudents(List students) { this.students = students; } } 四、开始整合SpringBoot mybatis 整合myabtis的具体不再赘述,网上很多也很简单。 application.properties #server port set server.port: 8082 #mysql datasource set spring.datasource. spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.typeAliasesPackage=cn.edu.jxnu.entity mybatis.mapperLocations=classpath\:mapper/*.xml mybatis.config-location=classpath\:mybatis-config.xml spring.jackson.serialization.indent_output =true logging.level.org.springframework.web=WARN #logging.file = C\:\\web\\temp\\log\\log.log logging.level.org.springframework=WARN logging.level.cn.edu.jxnu.dao=INFO logging.level.cn.edu.jxnu.resource=WARN

mybatis-config.xml 【没干什么】

 

StudentMapper.xml mapper文件

 

 

id,name,sex,address,age select from student where id=#{id}

mapper接口

 

 

package cn.edu.jxnu.dao; import org.apache.ibatis.annotations.Param; import cn.edu.jxnu.entity.Student; /** * dao层数据操作接口 * * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:41:43 * @version: V1.0 * */ public interface StudentDao { Student getStudentById(@Param("id") Integer id); }

配置SpringBoot扫描dao【这个也是主启动类,学过SpringBoot应该都知道,不再赘述】

 

 

package cn.edu.jxnu; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; import org.springframework.stereotype.Controller; /** * 启动类 * * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:41:24 * @version: V1.0 * */ @Controller @MapperScan("cn.edu.jxnu.dao") @SpringBootApplication // / 没有这个rest失效 只存在soap @ImportResource(locations = { "classpath:cxf-config.xml" }) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } cxf-config.xml很重要,等等再说   五、重点  整合cxf webService【注意,不是restful,而是webservice 基于soap的】

 

cxf配置类【因为是使用SpringBoot,所以使用类和注解配置】

 

package cn.edu.jxnu; import javax.xml.ws.Endpoint; import org.apache.cxf.Bus; import org.apache.cxf.bus.spring.SpringBus; import org.apache.cxf.jaxws.EndpointImpl; import org.apache.cxf.transport.servlet.CXFServlet; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; import cn.edu.jxnu.serviceImpl.StudentServiceImpl; /** * * @author: liguobin @Description: @时间: 2018-3-7 下午4:11:57 @version: V1.0 * */ @Configuration public class CxfConfig { @Bean public ServletRegistrationBean newServlet() { return new ServletRegistrationBean(new CXFServlet(), "/cxf/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } /** * @return */ @Bean @Qualifier("studentServiceImpl") // 注入webService public Endpoint endpoint(StudentRestfulServiceImpl studentServiceImpl) { EndpointImpl endpoint = new EndpointImpl(springBus(), studentServiceImpl); endpoint.publish("/webService");// 暴露webService api,用在资源访问 return endpoint; } @Bean("jsonProvider") // 构造一个json转化bean,用于将student转化为json,因为后面需要用这个bean配置json转化,所以给他取个名 public JacksonJsonProvider getJacksonJsonProvider() { return new JacksonJsonProvider(); } } CXFServlet()是返回一个cxf Servlet,用来拦截cxf,这里传入拦截路径/cxf/* 也就是访问路径是: ip:port/cxf/这里填@path的值

此处为了说明我写了一个Spring Controller

 

 

package cn.edu.jxnu; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import cn.edu.jxnu.entity.Student; import cn.edu.jxnu.entity.Students; import cn.edu.jxnu.service.StudentService; import com.alibaba.fastjson.JSONObject; /** * Spring 前端控制 * * 通过这个返回正确,作对比 * * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:35:17 * @version: V1.0 * */ @Controller public class SpringController { @Autowired private StudentService studentService; @ResponseBody @Produces({ MediaType.APPLICATION_JSON + "charset='utf-8'" }) @RequestMapping(value = "get/{id}", method = RequestMethod.GET) public String getStudent(@PathVariable("id") Integer id) { Student student = studentService.getStudent(id); Object json = JSONObject.toJSON(student); return json.toString(); } /** * @参数:{"ids":{"id":[1,2,3,4] * * @param ids * @return */ @ResponseBody @Produces({ MediaType.APPLICATION_JSON + "charset='utf-8'" }) @Consumes({ MediaType.APPLICATION_JSON }) @RequestMapping(value = "gets/{ids}", method = RequestMethod.GET) public String getAll(@PathVariable("ids") String ids) { Students students = studentService.getAllStudent(ids); Object json = JSONObject.toJSON(students); return json.toString(); } }

众所周知springmvc的默认由dispatherServlet处理。所以这里使用ip:port/get/1 即可获取【@PathVariable 是Spring的 与PathParam 是JSR-RS的功能类似】

 

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

此处只是为了说明cxf 的重要性。可以看到,没有使用/cxf/ 就依然是由springmvc来处理的

编写webservice 接口  基本是最简了

 

package cn.edu.jxnu.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.ws.rs.Consumes; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import cn.edu.jxnu.entity.Student; import cn.edu.jxnu.entity.Students; /** * @description WebService接口定义 soap * @author liguobin * */ @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) // 返回类型 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) // 请求类型 @WebService public interface StudentService { /** * 查找一个学生 * * @param id * @return */ @WebMethod public Student getStudent(@WebParam(name = "id") Integer id); /** * 查找多个学生 * * @param ids * @return */ @WebMethod public Students getAllStudent(@WebParam(name = "ids") String ids); }

实现service

 

 

package cn.edu.jxnu.serviceImpl; import java.util.ArrayList; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import cn.edu.jxnu.dao.StudentDao; import cn.edu.jxnu.entity.Student; import cn.edu.jxnu.entity.Students; import cn.edu.jxnu.service.StudentService; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; /** * 实现webservice接口,对外暴露 soap * * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:43:06 * @version: V1.0 * */ @Component//由Spring管理 @WebService(endpointInterface = "cn.edu.jxnu.service.StudentService") // webservice接口的全类名 public class StudentServiceImpl implements StudentService { /** * 注入spring bean */ @Autowired private StudentDao studentDao; @Override public Student getStudent(Integer id) { return studentDao.getStudentById(id); } /** * 没有测试正确性,不是本文重点 */ @Override public Students getAllStudent(String ids) { Students students = new Students(new ArrayList()); // 得到json对象 JSONObject json = JSONObject.parseObject(ids); // 获取对象的id列表 JSONArray sid = json.getJSONArray("id"); for (int i = 0; i < sid.size(); i++) { Integer s = sid.getInteger(0); if (s != null) { students.getStudents().add(studentDao.getStudentById(s)); } else { continue; } } return students; } }

测试webservice

 

输入http://localhost:8082/cxf    【查看所以soap和restful api】

单独查看某一个soap api【http://localhost:8082/cxf/webService?wsdl】 在前文  endpoint.publish(“/webService”);// 就是这个webService

浏览器显示:

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

点击wsdl

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

命名空间等等都是默认的,没有在webservice配置。也为了简洁

六、整合Restful api【 RESTful services】

首先,编写rest一个接口

 

package cn.edu.jxnu.resource; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import cn.edu.jxnu.entity.Student; import cn.edu.jxnu.entity.Students; /** * @author: liguobin * @Description: * @时间: 2018-3-7 下午3:59:15 * @version: V1.0 * */ @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public interface StudentInterface { /** * @param id * @return */ @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Path("/getone/{id:[0-9]{0,10}}") // 限制id只能是0~9的数组 不超过10位 public Student getStudent(@PathParam("id") Integer id); /** * 查找多个学生 * * @param ids * @return */ @GET @Produces({ MediaType.APPLICATION_JSON }) @Path("/getmany/{ids}") public Students getAllStudent(@PathParam("ids") String ids); }

实现rest接口

 

 

package cn.edu.jxnu.resource; import java.util.ArrayList; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.springframework.beans.factory.annotation.Autowired; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import cn.edu.jxnu.entity.Student; import cn.edu.jxnu.entity.Students; import cn.edu.jxnu.service.StudentService; @Path("/") public class StudentInterfaceImpl implements StudentInterface { @Autowired private StudentService studentService; // 获取json @Override @GET @Path("/getjson/{id:[0-9]{0,10}}") @Produces({ MediaType.APPLICATION_JSON }) public Student getStudent(@PathParam("id") Integer id) { return studentService.getStudent(id); } // 获取xml @GET @Path("/getxml/{id}") @Produces({ MediaType.APPLICATION_XML }) public Student getStudent2(@PathParam("id") Integer id) { return studentService.getStudent(id); } /** * 返回students集合,此方法没有测试,参数是一个json对象,该对象包含一个name为id的数组 */ @Override @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Path("/getmany/{ids}") public Students getAllStudent(@PathParam("ids") String ids) { Students students = new Students(new ArrayList()); // 得到json对象 JSONObject json = JSONObject.parseObject(ids); // 获取对象的id列表 JSONArray sid = json.getJSONArray("id"); for (int i = 0; i < sid.size(); i++) { Integer s = sid.getInteger(0); if (s != null) { students.getStudents().add(studentService.getStudent(s)); } else { continue; } } return students; } }

写法不标准甚至有错不要在意,不是本文重点

 

rest接口和类都写完,剩下就是配置了

前文,在主类曾经导入过一个配置文件,这个文件就是专门用来配置cxf的【@ImportResource(locations = { “classpath:cxf-config.xml” })】

cxf-config.xml

 

 

 

 

 

 

查看RestFul api

 

浏览器输入http://localhost:8082/cxf/ 就可以查看到多了 Available RESTful services:Endpoint address: http://localhost:8082/cxf/students

【因为soap和restful均是cxf处理】

最后测试restful请求

输入http://localhost:8082/cxf/students/getjson/1   获取json数据

SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

输入http://localhost:8082/cxf/students/getxml/1  获取xml数据SpringBoot整合CXF,实现Restful api 与 WebService api dao层使用Mybatis

github 源码:https://github.com/jxnu-liguobin/springboot-examples/tree/master/cxf

 

以上仅供参考,如有帮助到你,可以点个star

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由思创斯整理,转载请注明出处:https://ispacesoft.com/108132.html

赞 (0) 思创斯忠实用户-ss思创斯忠实用户-ss 0 0 生成海报


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3