简体   繁体   English

Spring MVC中的Java到JSON对象映射结果400错误的请求错误

[英]Java to JSON Object Mapping in Spring MVC results 400 Bad request error

I have some troubles when I recieve the json data from my application in app engine, some cases I recieve the json empty and in the log viewer of my application appears the following 当我从应用引擎中的应用程序接收json数据时遇到一些麻烦,某些情况下我将json接收为空,并且在应用程序的日志查看器中显示以下内容

    2014-07-03 16:46:21.193 /service/concentraciondeptos/ 400 43ms 0kb Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 module=default version=2
181.48.97.68 - - [03/Jul/2014:14:46:21 -0700] "GET /service/concentraciondeptos/ HTTP/1.1" 400 146 - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" "Instance ID" ms=44 cpu_ms=80 cpm_usd=0.000016 app_engine_release=1.9.6 trace_id=e2164f3bbeb2302c384164569b22b8dd 

and here's my code 这是我的代码

package com.servinfo.services.controller;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.servinfo.database.Manager;
import com.servinfo.services.AbstractController;
import com.servinfo.services.entity.ResponseObject;
import com.servinfo.services.entity.afe.ConcentracionDeptos;

@Controller
@RequestMapping("service/concentraciondeptos/*")
public class ConcentracionDeptosController {

    // private static final Logger log = Logger.getLogger("AreaTController");

    protected final static String RESPONSE_FORMAT = "application/json";

    private final static String GET = "";

    private static String STATE = "";

    private static String MESSAGE = "";


    @RequestMapping(value = GET, method = RequestMethod.GET, produces = RESPONSE_FORMAT)
    public @ResponseBody
    ResponseEntity<ResponseObject> getPoint() {

        if (getData().isEmpty()) {
            STATE = "ERROR";
            MESSAGE = ResponseObject.MSG_EMPTY;
        }

        // log.warning("Servicio areat");
        ResponseObject response = new ResponseObject(STATE, MESSAGE, getData());

        if (response.getStatus().equals("OK")) {
            return new ResponseEntity<ResponseObject>(response,
                    AbstractController.getHeader(), HttpStatus.OK);
        }

        return new ResponseEntity<ResponseObject>(response,
                AbstractController.getHeader(), HttpStatus.BAD_REQUEST);

    }

    public List<ConcentracionDeptos> getData() {
        STATE = "ERROR";
        MESSAGE = ResponseObject.MSG_ERROR;
        int intContador = 0;

        List<ConcentracionDeptos> result = new ArrayList<ConcentracionDeptos>();
        try {
            String sql = "select depto.coddane "
                    + ""
                    + "from departamentos as depto, proyecto as p, aporte_depto as apdepto,"
                    + "aporte_muni as apomuni, ciudades as ciu"
                    + " "
                    + "where apdepto.nit = p.nit and apdepto.id_proyecto = p.id "
                    + "and apdepto.coddane_depto = depto.coddane and apomuni.nit = p.nit "
                    + "and apomuni.id_proyecto = p.id and apomuni.coddane_ciudad = ciu.coddane";

            Manager mng = new Manager();
            ResultSet rs = mng.executeQuery(sql);
            if (rs.first()) {
                rs.beforeFirst();
                STATE = "OK";
                MESSAGE = ResponseObject.MSG_SUCCESS;
            } else {
                Thread.sleep(200);
                rs = mng.executeQuery(sql);
                if (rs.first()) {
                    rs.beforeFirst();
                    STATE = "OK";
                    MESSAGE = ResponseObject.MSG_SUCCESS;
                } else {
                    STATE = "ERROR";
                    MESSAGE = ResponseObject.MSG_ERROR;
                }
            }
            ConcentracionDeptos obEntity = new ConcentracionDeptos();
            while (rs.next()) {
                intContador++;
                obEntity.setCoddane(rs.getString("coddane"));
            }
            obEntity.setCantidadP(Integer.toString(intContador));
            result.add(obEntity);
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

when I execute the sql command in mysql I retrieve the correct information. 当我在mysql中执行sql命令时,我会检索正确的信息。 and here's th connection code 这是连接代码

import java.sql.*;
import com.google.appengine.api.utils.SystemProperty;

public class Manager {

    public ResultSet executeQuery(String sql){

        try{
            String url = null;
            if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
                Class.forName("com.mysql.jdbc.GoogleDriver");
                url = "jdbc:google:mysql://ID:App ID/DB?user=root";
            } else {
                Class.forName("com.mysql.jdbc.Driver");
                url = "jdbc:mysql://IP:3306/afe_services?user=root&password=****";
            }

            Connection conn = DriverManager.getConnection(url);
            ResultSet rs = conn.createStatement().executeQuery(sql);
            return rs;

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

what I'm doing bad? 我在做什么不好?

Thanks. 谢谢。

EDIT 2: 编辑2:

After lengthy discussion the issue here is that there is no database connection and maintenance of session. 经过长时间的讨论,这里的问题是没有数据库连接和会话维护。

Thus when you call the database, it fails, thus the no connection error. 因此,当您调用数据库时,它将失败,从而导致无连接错误。

You need to setup your connection something like described in the spring.io/guides page. 您需要像spring.io/guides页面中所述设置连接。 The web tutorial gives indepthy database connection and maintenance explanation. 该网络教程提供了深入的数据库连接和维护说明。

for quick explanation or dirty version setup you can check out http://spring.io/guides/gs/relational-data-access/ 有关快速说明或脏版本设置的信息,请访问http://spring.io/guides/gs/relational-data-access/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM