源码下载
http://download.csdn.net/download/knight_black_bob/9434283
修改后代码
http://download.csdn.net/download/knight_black_bob/9435077
效果图
http://localhost:8080/RepositoryPic/WebService/
http://localhost:8080/RepositoryPic/WebService/rest/picRepository/users/1/pics/1
http://localhost:8080/RepositoryPic/WebService/rest/showPic/users/1/pics/aaa.png
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.repository</groupId> <artifactId>RepositoryPic</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RepositoryPic Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> <!-- <spring.version>3.2.8.RELEASE</spring.version> --> <spring.version>4.1.2.RELEASE</spring.version> <servlet.api.version>2.5</servlet.api.version> <slf4j.version>1.7.5</slf4j.version> <cxf.version>2.7.12</cxf.version> <jackson.version>2.1.4</jackson.version> <commons.version>2.2</commons.version> </properties> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons.version}</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- SPRING JDBC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.transaction</groupId> <artifactId>transaction-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- spring begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <!-- @Inject begin --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- @Inject end --> <!-- servlet begin --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.api.version}</version> </dependency> <!-- servlet end --> <!-- cxf begin --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf.version}</version> <exclusions> <exclusion> <groupId>asm</groupId> <artifactId>asm-parent</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <!-- cxf end --> <!-- JSON begin --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.0</version> </dependency> <!-- JSON end --> </dependencies> <reporting> <plugins> <!-- findbugs plugin --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId> <version>3.0.3</version> <configuration> <xmlOutput>true</xmlOutput> <xmlOutputDirectory>target/site</xmlOutputDirectory> </configuration> </plugin> </plugins> </reporting> </project>
applicationContext-cxf-soap.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" default-autowire="byName"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <jaxws:endpoint implementor="cmcc.picrepository.webservice.impl.PicRepositorySoapServiceImpl" address="/picRepositorySoapService"></jaxws:endpoint> </beans>
applicationContext-cxf-rest.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <import resource="classpath*:META-INF/cxf/cxf.xml" /> <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" /> <jaxrs:server id="restServiceContainer" address="/rest"> <jaxrs:providers> <ref bean="jacksonProvider" /> </jaxrs:providers> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:serviceBeans> <ref bean="picRepositoryRestService" /> <ref bean="showPicRestService" /> </jaxrs:serviceBeans> <jaxrs:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxrs:inInterceptors> <jaxrs:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxrs:outInterceptors> </jaxrs:server> <bean id="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"></bean> <bean id="jacksonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"> <property name="mapper" ref="jacksonObjectMapper" /> </bean> </beans>
package cmcc.picrepository.restfulservice; import javax.annotation.Resource; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import org.springframework.stereotype.Component; import cmcc.picrepository.dto.PicRepository; import cmcc.picrepository.dto.PicRepositorys; import cmcc.picrepository.webservice.PicRepositorySoapService; /** * @author baoyou E-mail:[email protected] * @version 2016年2月16日 下午1:47:41 * * desc: ... */ @Path(value = "/picRepository") @Produces({ MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_XML}) @Component public class PicRepositoryRestService { @Resource PicRepositorySoapService picRepositorySoapService; @GET @Produces({ MediaType.APPLICATION_XML}) @Path("/users/{userId}/pics/{picRepositoryId}") public PicRepository getPicRepository(@PathParam("userId")int userId,@PathParam("picRepositoryId")int picRepositoryId) { PicRepository picRepository =picRepositorySoapService.get(picRepositoryId); if (picRepository == null) { ResponseBuilder builder = Response.status(Status.NOT_FOUND); builder.type("application/xml"); builder.entity("<errorMsg>PicRepository with id:" + picRepositoryId+ " can not be found!</errorMsg>"); throw new WebApplicationException(builder.build()); } else { System.out.println("PicRepository with id:" + picRepositoryId+ " is found"); return picRepository; } } @POST @Consumes({ MediaType.APPLICATION_XML}) @Path("/add") public Response addPicRepository(PicRepository picRepository) { picRepositorySoapService.add(picRepository); return Response.ok(picRepository).build(); } @DELETE @Consumes({ MediaType.APPLICATION_XML}) @Path("/delete/{picRepositoryId}") public Response deletePicRepository(@PathParam("picRepositoryId")int picRepositoryId) { return null; } @PUT @Path("/put") @Consumes({ MediaType.APPLICATION_XML}) public Response updatePicRepository(PicRepository picRepository) { return null; } @GET @Path("/getAlls") @Produces({ MediaType.APPLICATION_XML}) public PicRepositorys getPicRepositorys() { return null; } }
package cmcc.picrepository.restfulservice; import java.io.File; 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 javax.ws.rs.core.Response; import org.springframework.stereotype.Component; /** * @author baoyou E-mail:[email protected] * @version 2016年2月17日 下午2:26:59 * * desc: ... */ @Path(value = "/showPic") @Produces({ MediaType.APPLICATION_XML}) @Consumes({ MediaType.APPLICATION_XML}) @Component public class ShowPicRestService { @GET @Produces("image/*") @Path("/users/{userId}/pics/{picName}") public Response getInputStream(@PathParam("userId")int userId,@PathParam("picName")String picName) { File file = new File("C:\\Users\\cmcc-B100036\\Desktop\\1.png"); return Response.ok(file,"image/jpg").build(); } }
package cmcc.picrepository.webservice; import java.util.List; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import cmcc.picrepository.dto.PicRepository; /** * @author baoyou E-mail:[email protected] * @version 2016年2月18日 上午9:51:35 * * desc: ... */ @WebService @SOAPBinding(style = Style.RPC) public interface PicRepositorySoapService { public List<PicRepository> getAlls(); public void del(Integer picRepositoryId); public PicRepository get(Integer picRepositoryId); public void update(PicRepository picRepository); public void add(PicRepository picRepository); }
package cmcc.picrepository.webservice.impl; import java.util.List; import javax.annotation.Resource; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import org.springframework.stereotype.Service; import cmcc.picrepository.dao.PicRepositoryDao; import cmcc.picrepository.dto.PicRepository; import cmcc.picrepository.webservice.PicRepositorySoapService; /** * @author baoyou E-mail:[email protected] * @version 2016年2月18日 上午9:54:43 * * desc: ... */ @Service @WebService(endpointInterface="cmcc.picrepository.webservice.PicRepositorySoapService", serviceName="PicRepositorySoapService") @SOAPBinding(style = Style.RPC) public class PicRepositorySoapServiceImpl implements PicRepositorySoapService{ @Resource PicRepositoryDao picRepositoryDao; @Override public List<PicRepository> getAlls() { return null; } @Override public void del(Integer picRepositoryId) { // TODO Auto-generated method stub } @Override public PicRepository get(Integer picRepositoryId) { return picRepositoryDao.getPicRepositoryById(picRepositoryId); } @Override public void update(PicRepository picRepository) { // TODO Auto-generated method stub } @Override public void add(PicRepository picRepository) { // TODO Auto-generated method stub } }
package cmcc.picrepository.util; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.bind.annotation.adapters.XmlAdapter; public class DateAdapter extends XmlAdapter<String, Date> { private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public String marshal(Date v) throws Exception { return dateFormat.format(v); } @Override public Date unmarshal(String v) throws Exception { return dateFormat.parse(v); } }
package cmcc.picrepository.dto; import java.io.Serializable; import java.util.Date; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import cmcc.picrepository.util.DateAdapter; /** * @author baoyou E-mail:[email protected] * @version 2016年2月15日 上午11:25:20 * * desc: ... */ @XmlRootElement(name = "PicRepository") @XmlAccessorType(XmlAccessType.FIELD) public class PicRepository implements Serializable{ private static final long serialVersionUID = 1L; // id,origin_name,path,user_id,link,insert_time,last_update_time,desc,nextfix,pic_size private int id; private String originName; private String path; private int userId; private String link; @XmlJavaTypeAdapter(DateAdapter.class) private Date insertTime; @XmlJavaTypeAdapter(DateAdapter.class) private Date lastUpdateTime; private String description; private String nextfix; private int picSize; public int getId() { return id; } public String getOriginName() { return originName; } public String getPath() { return path; } public int getUserId() { return userId; } public String getLink() { return link; } public Date getInsertTime() { return insertTime; } public Date getLastUpdateTime() { return lastUpdateTime; } public String getDescription() { return description; } public String getNextfix() { return nextfix; } public int getPicSize() { return picSize; } public void setId(int id) { this.id = id; } public void setOriginName(String originName) { this.originName = originName; } public void setPath(String path) { this.path = path; } public void setUserId(int userId) { this.userId = userId; } public void setLink(String link) { this.link = link; } public void setInsertTime(Date insertTime) { this.insertTime = insertTime; } public void setLastUpdateTime(Date lastUpdateTime) { this.lastUpdateTime = lastUpdateTime; } public void setDescription(String description) { this.description = description; } public void setNextfix(String nextfix) { this.nextfix = nextfix; } public void setPicSize(int picSize) { this.picSize = picSize; } }
package cmcc.picrepository.dao.impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import cmcc.picrepository.base.dao.JdbcTempBaseDao; import cmcc.picrepository.dao.PicRepositoryDao; import cmcc.picrepository.dto.PicRepository; /** * @author baoyou E-mail:[email protected] * @version 2016年2月15日 下午2:59:54 * * desc: ... */ @Repository public class PicRepositoryDaoImpl extends JdbcTempBaseDao implements PicRepositoryDao{ @SuppressWarnings({ "rawtypes" }) private class PicRepositoryRowMapper implements RowMapper { @Override public PicRepository mapRow(ResultSet rs, int i) throws SQLException { PicRepository pr = new PicRepository(); pr.setId(rs.getInt("id")); pr.setOriginName(rs.getString("origin_name")); pr.setPath(rs.getString("path")); pr.setUserId(rs.getInt("user_id")); pr.setLink(rs.getString("link")); pr.setInsertTime(rs.getDate("insert_time")); pr.setLastUpdateTime(rs.getDate("last_update_time")); pr.setDescription(rs.getString("description")); pr.setNextfix(rs.getString("nextfix")); pr.setPicSize(rs.getInt("pic_size")); return pr; } } @SuppressWarnings("unchecked") @Override public List<PicRepository> getAlls() { String sql = "select * from picrepository"; return this.getJdbcTemplate().query(sql, new PicRepositoryRowMapper()); } @SuppressWarnings("unchecked") @Override public PicRepository getPicRepositoryById(int picRepositoryId) { String sql = "select * from picrepository where id=" + picRepositoryId; List<PicRepository> userList = this.getJdbcTemplate().query(sql, new PicRepositoryRowMapper()); if (userList !=null && userList.size() > 0 ) { return userList.get(0); }else{ return null; } } @Override public int savePicRepository(PicRepository picRepository) { String sql = "insert into picrepository(origin_name,path,user_id,link,insert_time,last_update_time,description,nextfix,pic_size) values (?,?,?,?,?,?,?,?,?)"; return this.getJdbcTemplate().update(sql, new Object[] { picRepository.getOriginName() ,picRepository.getPath() ,picRepository.getUserId() ,picRepository.getLink() ,picRepository.getInsertTime() ,picRepository.getLastUpdateTime() ,picRepository.getDescription() ,picRepository.getNextfix() ,picRepository.getPicSize() }); } }
package cmcc.picrepository.base.dao; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.support.JdbcDaoSupport; /** * @author baoyou E-mail:[email protected] * @version 2016年2月17日 上午11:42:53 * * desc: ... */ public class JdbcTempBaseDao extends JdbcDaoSupport { @Resource(name = "jdbcTemplate") public JdbcTemplate jdbcTemplate; @PostConstruct public void initSqlMapClient() { super.setJdbcTemplate(jdbcTemplate); } }