@ResponseBody

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@ResponseBody相关的知识,希望对你有一定的参考价值。

package com.yundaex.wms.interfaces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 com.yundaex.common.basic.vo.RequestVO;
import com.yundaex.common.basic.vo.SimpleResponseVO;
import com.yundaex.common.exception.WrappedRuntimeException;
import com.yundaex.common.validation.asserts.YDAssert;
import com.yundaex.wms.basicdata.constant.CommandTypeConstants;
import com.yundaex.wms.inbound.handler.PutawayCompleteHandler;
import com.yundaex.wms.inventory.handler.ConsMoveCompleteHandler;
import com.yundaex.wms.inventory.handler.MoveCompleteHandler;
import com.yundaex.wms.outbound.handler.OnhPackFinishHandler;
import com.yundaex.wms.outbound.handler.WrhMainpickFinishHandler;
import com.yundaex.wms.outbound.handler.WrhPartpickFinishHandler;

/**
 * 
 * <pre>
 *   Title: ServicesForWMSSocket.java
 *   Description: 
 *   Copyright: yundaex.com Copyright (c) 2017
 *   Company: 上海韵达货运有限公司
 * </pre>
 * 
 * @author tonglele
 * @version 1.0
 * @date 2017年10月10日
 */
@Controller
@RequestMapping(method = { RequestMethod.POST, RequestMethod.GET }, value = "/external", produces = "application/json;charset=UTF-8")
public class ServicesForWMSSocket {

    @Autowired
    private WrhMainpickFinishHandler wrhMainpickFinishHandler;
    
    @Autowired
    private OnhPackFinishHandler onhPackFinishHandler;
    
    @Autowired
    private WrhPartpickFinishHandler wrhPartpickFinishHandler;

    @Autowired
    private PutawayCompleteHandler putawayCompleteHandler;
    
    @Autowired
    private ConsMoveCompleteHandler consMoveCompleteHandler;

    @Autowired
    private MoveCompleteHandler moveCompleteHandler;
    
    @RequestMapping(value = "/serviceForWMSSocket.do")
    @ResponseBody
    public Object totalProcesser(@RequestBody RequestVO vo) throws Exception {
        YDAssert.isNotNull(vo, "请求对象不能为空");
        String method = vo.getMethod();
        Object data = vo.getData();
        YDAssert.isNotNull(method, "method字段不能为空");
        YDAssert.isNotNull(data, "data字段不能为空");
        
        SimpleResponseVO responseVO = new SimpleResponseVO();
        Object responseData = null;
        try {
            switch (method) {
            case CommandTypeConstants.WRH_MAINPICK_FINISH:
                responseData = wrhMainpickFinishHandler.propagation(data);
            //取消分拣完成接口    
            /*case CommandTypeConstants.WRH_PARTPICK_FINISH:
                return wrhPartpickFinishHandler.propagation(data);*/
            case CommandTypeConstants.ONH_PACK_FINISH:
                responseData = onhPackFinishHandler.propagation(data);
            case CommandTypeConstants.PTH_PUTAWAY_COMPLETE:
                responseData = putawayCompleteHandler.propagation(data);
            case CommandTypeConstants.CMVH_CONS_MOVE_COMPLETE:
                responseData =consMoveCompleteHandler.propagation(data);
            case CommandTypeConstants.MVH_MOVE_COMPLETE:
                responseData = moveCompleteHandler.propagation(data);
            default:
                YDAssert.isTrue(false, "没有定义该接口" + method);
            }
            responseVO.setData(responseData);
            responseVO.setResult("TRUE");
        } catch (Exception e) {
            String excMsg = e.getMessage();
            String errorCode = "VLD_ILG_ARG";//"参数校验不合法"
            if (e instanceof WrappedRuntimeException) {
                excMsg = ((WrappedRuntimeException)e).getErrorDetails().getExceptionList().get(0);
                errorCode = ((WrappedRuntimeException)e).getErrorCode();
            }
            responseVO.setData(responseData);
            responseVO.setResult("FALSE");
            responseVO.setErrorCode(errorCode);
            responseVO.setRemark(excMsg);
        }
        return responseVO;
    }
}

wms-springmvc-servlet.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:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
    
    <mvc:annotation-driven>
        <mvc:argument-resolvers> 
            <bean class="com.yundaex.common.bidiboxing.convert.support.PayloadToBeanArgumentResolver"/> 
        </mvc:argument-resolvers> 
        <mvc:return-value-handlers>
            <bean class="com.yundaex.common.bidiboxing.convert.support.BeanToPayloadReturnTypeResolver" />
        </mvc:return-value-handlers>
        <mvc:message-converters register-defaults="true">  
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >  
                 <property name="supportedMediaTypes" value="*/*" /> 
            </bean>
        </mvc:message-converters>  
    </mvc:annotation-driven>
    
    
    <aop:aspectj-autoproxy proxy-target-class="true" />
    <context:annotation-config />
    <context:component-scan base-package="com.yundaex.wms">
    </context:component-scan>
    
</beans>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

 引用jar包可能spring提供,就不用加

 

public class SimpleResponseVO {
    
    /***
     * 处理结果,TURE/FALSE
     */
    private String result;
    
    /** 返回代码**/
    private String errorCode;
    
    /** 返回的信息**/
    private String remark;
    
    /** 返回的对象**/
    private Object data;

    public Object getResult() {
        return result;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public void setResult(String result) {
        this.result = result;
    }

    

}

 

以上是关于@ResponseBody的主要内容,如果未能解决你的问题,请参考以下文章

ajax请求 spring mvc responsebody 注解的方法 为啥写不了cookie

Spring编程:@ResponseBody 注解

spring mvc从@ResponseBody取到json发现中文乱码

Spring MVC @ResponseBody注解返回值中文乱码问题

springmvc @ResponseBody返回json 报406 not acceptable

springmvc使用ajax进行数据交互时,session失效问题(@ResponseBody与session能否同时使用?)