cxf +spring+mybatis如何传输json数据并返回json数据

qq_24597963 2017-06-21 10:05:40
1. 设计一个查询学生信息的Restful服务,该服务的输入输出为Json格式的参数。其中输入参数为学号,输出参数为学生名字、性别、地址等基本信息
2.Restful服务的实现框架必须为CXF,接口格式为Json;

现在根据一个int学号能查出一个student对象的一个结果。

这是webservice接口
package com.sdo.webservice.webservice;

import javax.jws.WebService;

import com.sdo.webservice.entity.Student;

@WebService
public interface StudentWebService {
Student getStudentById(int id);
}




这是webservice实现类
[code=java]

package com.sdo.webservice.webservice.impl;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdo.webservice.dao.StudentDao;
import com.sdo.webservice.entity.Student;
import com.sdo.webservice.webservice.StudentWebService;


@WebService(endpointInterface = "com.sdo.webservice.webservice.StudentWebService",serviceName="Student")
@Service
public class StudentWebServiceImpl implements StudentWebService{

@Resource
private StudentDao studentDao;

@Override
public Student getStudentById(int id) {
return studentDao.getStudentById(id);
}

}


这是mybatis的dao

package com.sdo.webservice.dao;

import com.sdo.webservice.entity.Student;

public interface StudentDao {
Student getStudentById(int id);
}



这是发布webservice的xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


<jaxws:endpoint id="Student" address="/StudentWebService"
implementor="com.sdo.webservice.webservice.impl.StudentWebServiceImpl" />

</beans>




这是数据源及其他bean的spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<!-- 采用注释的方式配置bean -->
<context:annotation-config/>
<!-- 配置要扫描的包 -->
<context:component-scan base-package="com.sdo.webservice"/>
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:config/init.properties"/>
<!-- 配置数据源 oracle c3p0-->
<bean id="dataSourceTarget" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:checkoutTimeout="${cpool.checkoutTimeout}"
p:minPoolSize="${cpool.minPoolSize}"
p:maxPoolSize="${cpool.maxPoolSize}"
p:maxIdleTime="${cpool.maxIdleTime}"
destroy-method="close"
></bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="dataSourceTarget"></property>
</bean>



<!-- 配置mybitasSqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/sqlmap-config.xml" />
<property name="mapperLocations" value="classpath:sqlMapper/*Mapper.xml" />
<property name="typeAliasesPackage" value="com.sdo.webservice.entity" />
</bean>
<!-- 配置dao层与mapper.xml关联 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.sdo.webservice.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>



web.xml
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml,
classpath:config/applicationContext-webservice.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>



这里写个客户端
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.sdo.webservice.entity.Student;
import com.sdo.webservice.webservice.StudentWebService;

public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
bean.setServiceClass(StudentWebService.class);
bean.setAddress("http://localhost:8080/CXF0621/StudentWebService");
StudentWebService studentWebService = (StudentWebService)bean.create();
Student student = studentWebService.getStudentById(1);
System.out.println(student.toString());
}
}
这是查询的结果
六月 21, 2017 9:44:50 上午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webservice.webservice.sdo.com/}StudentWebServiceService from class com.sdo.webservice.webservice.StudentWebService
Student [id=1, name=xrq, sex=男, address=gggg]




要让输入 和 输出都为json 要怎么做啊 新手求帮忙
...全文
432 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_24597963 2017-06-21
  • 打赏
  • 举报
回复
引用 2 楼 agony_sun 的回复:
这个是要独立完成的吧~~是某个公司的第二轮笔试题啊~~
问下怎么才能找到我想要的东西
qq_24597963 2017-06-21
  • 打赏
  • 举报
回复
引用 2 楼 agony_sun 的回复:
这个是要独立完成的吧~~是某个公司的第二轮笔试题啊~~
不过感觉求助的希望也不大
qq_24597963 2017-06-21
  • 打赏
  • 举报
回复
引用 2 楼 agony_sun 的回复:
这个是要独立完成的吧~~是某个公司的第二轮笔试题啊~~
没办法 独立不了 上网求助下
椰子是只猫 2017-06-21
  • 打赏
  • 举报
回复
这个是要独立完成的吧~~是某个公司的第二轮笔试题啊~~
qq_24597963 2017-06-21
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧