1、web.xml的修改

  <servlet>
		<servlet-name>rest</servlet-name>
		<servlet-class>com.paic.esg.rpc.protocol.springrest.SpringEsgDispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>


	<servlet-mapping>
		<servlet-name>rest</servlet-name>
		<url-pattern>/appsvr/*</url-pattern>
	</servlet-mapping>
    <session-config>



2、新增spring-servlet.xm

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
		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://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


	<!-- 自动搜索@Controller标注的类,包括其下面的子包 -->
	<context:component-scan base-package="com.paic.pecp.manage.resful" />


	<!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml -->
	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<!-- 设置为true以忽略对Accept Header的支持 -->
		<property name="ignoreAcceptHeader" value="true" />
		
		<!-- 在没有扩展名时即: "/blog/1" 时的默认展现形式 -->
		<property name="defaultContentType" value="text/html" />


		<!-- 扩展名至mimeType的映射,即 /blog.json => application/json -->
		<property name="mediaTypes">
			<map>
				<entry key="html" value="text/html" />
				<entry key="pdf" value="application/pdf" />
				<entry key="xsl" value="application/vnd.ms-excel" />
				<entry key="xml" value="application/xml" />
				<entry key="json" value="application/json" />
			</map>
		</property>
	
		<!-- 用于开启 /blog/123?format=json 的支持 -->
		<property name="favorParameter" value="false" />
		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
				<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
					<property name="prefix" value="/pages" />
					<property name="suffix" value=".jsp"></property>
				</bean>
			</list>
		</property>
		<property name="defaultViews">
			<list>
				<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
			</list>
		</property>
	</bean>
	</beans>

3、resful接口类

package com.paic.pscp.resful.resfulmgr.web.control;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.paic.esg.common.extension.PA_API;
import com.paic.pafa.app.dto.ServiceRequest;
import com.paic.pafa.app.dto.ServiceResponse;
import com.paic.pafa.app.lwc.core.util.DevLog;
import com.paic.pafa.app.web.servlet.ModelAndView;
import com.paic.pafa.app.web.servlet.mvc.AbstractController;
import com.paic.pscp.manager.common.web.util.CommonWebContextNames;
import org.apache.commons.beanutils.DynaBean;

/**
 * 服务器端RESFUL接口查询待发实现类
 */
@Controller
@PA_API(value="PSCP_PROVIDE_SEND")
public class ResfulSendMessageController  extends AbstractController {
	@RequestMapping(value ="/public/smg/send", method = {RequestMethod.POST})
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DevLog.info("--------resful服务端口接收开始--------");
		String requestContent = null;
		Map model = new HashMap();
		 InputStream in =  request.getInputStream();
		 BufferedReader rd = new BufferedReader(new InputStreamReader(in,"UTF-8"));
         String tempLine = rd.readLine();
         StringBuffer tempStr = new StringBuffer();
         String crlf=System.getProperty("line.separator");
         while (tempLine != null)
         {
             tempStr.append(tempLine);
             tempStr.append(crlf);
             tempLine = rd.readLine();
         }
         requestContent = tempStr.toString();
         JSONObject js =null;
        //try{
        	js =JSONObject.fromObject(requestContent);
        //}catch (Exception e) {
		//	e.printStackTrace();
		//}
        Map m = JSONObject.fromObject(js);
		ServiceRequest serviceRequest = new ServiceRequest();
		serviceRequest.setRequestedServiceID("sendMessageAction");
		serviceRequest.setCurrentRequestObject(m);
		ServiceResponse serviceResponse = this.dispatchService(serviceRequest,
				CommonWebContextNames.PAFA_AC);
		response.setContentType("text/json;charset=UTF-8");
		response.getWriter().write(
				JSONObject.fromObject(serviceResponse.getModel()).toString());
		response.getWriter().flush();
		DevLog.info("--------resful服务端口接收结束--------");
		return null;
	}

}


Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐