SPRING-REST和CXF-REST实现过程中的系列问题

阅读更多
1、mybatis 数字用$,字符用#

2、cxf 关于JSONProvider 返回单个对象数组的问题。
app-rest.xml配置


	
	
	

	
		app
		comment
	

	
		application/json
		application/jettison
	

	
		
		
		
		
	

	
		
			
			
		
		
			
			
			
			
			
			
			
			
			
		
		
			
		
	


javabean代码
import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

import sgh.comment.base.model.Comments;

//评论列表
@XmlAccessorType(XmlAccessType.FIELD)
public class CommentsResult implements Serializable {

	private static final long serialVersionUID = -6304211106650057160L;

	/**
	 * 总条数
	 */
	public Integer totalCount;
	
	/**
	 * 总页数
	 */
	public Integer totalPage;
	
	/**
	 * APP评论
	 */
	@XmlElement(name="comment")
	public List commentList;
}

相关源代码参见截图:
SPRING-REST和CXF-REST实现过程中的系列问题_第1张图片
SPRING-REST和CXF-REST实现过程中的系列问题_第2张图片
SPRING-REST和CXF-REST实现过程中的系列问题_第3张图片
备注:
a.arrayKeys和serializeAsArray组合使用,serializeAsArray=true表示arrayKeys中提到的属性值都以数组形式返回,解决单条数据不以数组形式返回的问题。
b.arrayKeys是值需要以数组形式返回的属性名字。如上是comment而不是commentList。

4、cxf关于数字类型的字符串自动被去掉引号的问题。
具体原因参见:org.codehaus.jettison.mapped.DefaultConverter和org.codehaus.jettison.mapped.SimpleConverter
方法1:SimpleConverter的实现去重写DefaultConverter。
方法2:修改配置使其默认用SimpleConverter即可。具体怎么配置目前还不知道。

5、spring mvc rest空对象也被返回的问题。

	
		
		
			
			
				
					
					
						NON_NULL
					
				
			
		
		
		
		
			
			
				
					
					
						NON_NULL
					
				
			
		
		
		
		
	



6、spring mvc rest支持jsonp的问题。









	
		
		
		
	


重写MappingJacksonJsonpView 类
package org.springframework.web.servlet.view.jsonp;

import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class MappingJacksonJsonpView extends MappingJacksonJsonView {

	/**
	 * Default content type. Overridable as bean property.
	 */
	public static final String DEFAULT_CONTENT_TYPE = "application/javascript";

	@Override
	public String getContentType() {
		return DEFAULT_CONTENT_TYPE;
	}

	/**
	 * Prepares the view given the specified model, merging it with static
	 * attributes and a RequestContext attribute, if necessary. Delegates to
	 * renderMergedOutputModel for the actual rendering.
	 * 
	 * @see #renderMergedOutputModel
	 */
	@Override
	public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if ("GET".equals(request.getMethod().toUpperCase())) {
			@SuppressWarnings("unchecked")
			Map params = request.getParameterMap();
			if (params.containsKey("callback")) {
				response.getOutputStream().write(new String(params.get("callback")[0] + "(").getBytes());
				super.render(model, request, response);
				response.getOutputStream().write(new String(");").getBytes());
				response.setContentType("application/javascript");
			} else {
				super.render(model, request, response);
			}
		} else {
			super.render(model, request, response);
		}
	}
}


7、cxf rest 支持jsonp的问题。
参见: http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-JSONWithPadding%28JSONP%29
One option for supporting "JSON With Padding" (JSONP) is to extend the default JSONProvider and override its writeTo method as follows:
@Produces("application/javascript")
class JsonpProvider extends JSONProvider {
		
    @Override
    public void writeTo(Object obj, Class cls, Type genericType, Annotation[] anns, MediaType m, MultivaluedMap headers,
	OutputStream os) throws IOException {
	String prefix = getContext().getHttpServletRequest().getParameter("_jsonp");
	boolean hasPrefix = !isEmpty(prefix);
	if(hasPrefix) {
	    os.write(prefix.getBytes(HttpUtils.getSetEncoding(m, headers, "UTF-8")));
	    os.write('(');
	}
	super.writeTo(obj, cls, genericType, anns, m, headers, os);
	if(hasPrefix) {
		os.write(')');
	}
    }
}

Similar approach can work when Jackson is used.
Alternatively, a custom servlet filter can be used to support JSONP. Please read the Supporting JSONP blog post for more information.
Starting from CXF 2.3.4 and 2.4.0, JSONP can be supported with the help of CXF in/out interceptors:

    
      
    		  

    
       
    
    
       
       
     


JsonpInInterceptor checks if a JSONP callback query parameter is available (default is _jsonp) and if yes then it saves its value on the current exchange for out interceptors to know if they have to write the paddings or not. The name of the expected callback parameter can be customized. JsonpPreStreamInterceptor and JsonpPostStreamInterceptor ensure the actual JSON stream is 'padded' properly. JsonpPreStreamInterceptor will also set Content-Type to "application/x+javascript" by default but this can be changed:

    
      
    		  

    
       
          
        
    
    
       
           
       
       
           
           
       
     



8、关于ThreadLocal与线程池的问题。
Web服务器使用线程池,所以会有重用线程的问题,而ThreadLocal是根据线程ID来区分的。
http://blog.csdn.net/comliu/article/details/3186778
http://hi.baidu.com/sqxxr/item/e9946b34427c5ec02e8ec294

9、安装tomcat支持apr
cd /usr/local/apache-tomcat-6.0.18/bin/tomcat-native-1.1.14-src/jni/native/
 ./configure --prefix=/usr/local/apr
 ./configure --with-apr=/usr/local/apr
 make
 make install

http://blog.163.com/chhj_292/blog/static/12229472008101434124476/
  • SPRING-REST和CXF-REST实现过程中的系列问题_第4张图片
  • 大小: 44.9 KB
  • SPRING-REST和CXF-REST实现过程中的系列问题_第5张图片
  • 大小: 43.8 KB
  • SPRING-REST和CXF-REST实现过程中的系列问题_第6张图片
  • 大小: 41.7 KB
  • 查看图片附件

你可能感兴趣的:(SPRING-REST和CXF-REST实现过程中的系列问题)