WebService接口出入参以XML形式传参

步骤1:首先配置pom.xml引入依赖包

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.dataformat</groupId>
			<artifactId>jackson-dataformat-xml</artifactId>
			<version>2.11.0</version>
		</dependency>
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>mssql-jdbc</artifactId>
			<scope>runtime</scope>
		</dependency>

步骤2:配置文件application.properties

server.port=8080

spring.datasource.driverClassName:com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=HO20220504
spring.datasource.username=sa
spring.datasource.password=123456

步骤3:写实体类,分别按照入参出参配置实体类并生成get和set方法。这里不赘述。
步骤4:写service类以及其实现类。

package com.sinqi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.sinqi.bo.Patient;
@Repository
public class dbserviceimpl {
	@Autowired
	JdbcTemplate mssql ;
	public List<Patient> getPat(String patientName,String idCard,String cardNo){
		RowMapper<Patient> map_pat = new BeanPropertyRowMapper<Patient>(Patient.class);
		//通过入参拼sql语句。
		String sql = "select table05 as patientName, table01 as patientId ,table15 as idCard , "
				+ "table02 as cardNo ,vaa35 as patientPhone, \r\n" + 
				"vaa10 as patientAge  , case abw01 when '1' then '男' when '2' then '女' else '未知' end as patientGender\r\n" + 
				"from table1 \r\n" + 
				"where vaa05 like '%"+patientName+"%' and vaa15 like '%"+idCard+"%' and vaa02 like '%"+cardNo+"%' "; 
				List<Patient> list_pat = mssql.query(sql, map_pat);
				return  list_pat ;
	}
}

service类

package com.sinqi.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sinqi.bo.Patient;
import com.sinqi.service.impl.dbserviceimpl;

@Service
public class dbservice {

	@Autowired
	dbserviceimpl impl ;
	
	public List<Patient> getPat(String patientName,String idCard,String cardNo){
		return impl.getPat(patientName, idCard, cardNo);
	}
}

步骤5:写controller类

package com.sinqi.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.sinqi.bo.PatInfo;
import com.sinqi.bo.Patient;
import com.sinqi.service.dbservice;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TeacherController {

	@Autowired
	dbservice service ;
	produces= {"application/xml;charset=utf-8"} 这句作用是以xml形式返回,如果不写就以json格式返回了。
	@RequestMapping(value="/patient" ,method=RequestMethod.POST,produces= {"application/xml;charset=utf-8"})
	@ResponseBody
	public List<Patient> getparent(@RequestBody PatInfo pat){
		String name = pat.getPatientName() ;
		String idcard = pat.getIdCard() ;
		String cardno = pat.getCardNo() ;
		List<Patient> list_pat = service.getPat(name, idcard, cardno) ;
		System.out.println("方法进来了");
		return list_pat ;
	}

}

步骤6:使用postman进行测试

在这里插入图片描述
这是已xml形式传入参数

在这里插入图片描述

也可以json格式传参。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinqi_SG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值