Java - @ResponseBody String - 错误200

时间:2016-11-14 15:58:45

标签: java spring spring-boot

我正在尝试从我的控制器中获取string response,但我收到以下错误:

  

SyntaxError:JSON输入的意外结束(...)“错误200”

当我将响应更改为布尔值或其他类型时,它正常工作。问题是当我尝试返回一个字符串时。

js code:

 $.ajax({
   method: "POST",
   url: "./signup",
   data: _data,
   dataType: "json",
   contentType : "application/json;charset=UTF-8",
   success : function(data) {
       console.log(data)
   },
   error : function(qXHR, textStatus, errorThrown){
       console.log(errorThrown, "Error " + qXHR.status);
   }
  });

控制器代码:

@RequestMapping(value = "/signup", method = RequestMethod.POST, produces = {"text/plain", "application/*"})
    public @ResponseBody String signup(@RequestBody UserSignup details) {
       //...
        return message;
    }

任何想法如何解决这个问题?我尝试过一些东西,但没有任何效果。我认为响应格式是错误的,因为代码期望。

修改

我已更改了我的代码(已移除produces),但我仍然遇到同样的错误:

  

SyntaxError:JSON输入的意外结束(...)“错误200”

@RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody String signup(@RequestBody UserSignup details) {
        message = "ok";
        }
        return message;
    }

3 个答案:

答案 0 :(得分:5)

你的方法错了。您要说的是生成 produce = {“text / plain”,“application / *”} 但您还要添加@ResponseBody,它将生成JSON格式响应。

我建议你删除属性产生。并验证您返回的字符串是否格式良好

答案 1 :(得分:0)

尝试将您的响应包装在ResponseEntity类

this.createGeometryFromBufferGeometry = function(matrix, mesh) {

  var geom = mesh.geometry;

  var decalGeometry = new THREE.Geometry(); 

  var projectorInverse = matrix.clone().getInverse(matrix);
  var meshInverse = mesh.matrixWorld.clone().getInverse(mesh.matrixWorld);
  var faces = [];

  for(var i = 0; i < geom.attributes.position.array.length; i+=9){

    var pts = [];
    var valid = false;

    for(var v = 0; v < 9; v+=3) {


      var vec = new THREE.Vector3(geom.attributes.position.array[i+v],geom.attributes.position.array[i+v+1],geom.attributes.position.array[i+v+2]);
      console.log((i+v) + " " + (i+v+1) + " " + (i+v+2) );
      console.log(vec);

      vec.applyMatrix4(mesh.matrixWorld);
      vec.applyMatrix4(matrix);

      if((vec.z > 1) || (vec.z < -1) || (vec.x > 1) || (vec.x < -1) || (vec.y > 1) || (vec.y < -1)) {
      } else {
        valid = true;
      }

      pts.push(vec);
    }


    if(valid) {

      var uv = [];
      for(var n = 0; n < 3; n++){
        uv.push(new THREE.Vector2( (pts[n].x + 1) / 2, (pts[n].y + 1) / 2));

        pts[n].applyMatrix4(projectorInverse);
        pts[n].applyMatrix4(meshInverse);

        decalGeometry.vertices.push( pts[n] );
      }

      decalGeometry.faceVertexUvs[0].push(uv);

      var newFace = new THREE.Face3()

      newFace.a = decalGeometry.vertices.length - 3;
      newFace.b = decalGeometry.vertices.length - 2;
      newFace.c = decalGeometry.vertices.length - 1;

      decalGeometry.faces.push(newFace);
    }

  }
  return decalGeometry;
}

还要仔细检查您要发送到服务器的数据,也许这就是问题,您能告诉我们_data值吗?

答案 2 :(得分:0)

因为当问题与String不同时,我没有问题,因此我解决了创建自己对象的问题。以下是代码:

public class Response<T> {
    private T message;
    private Exception ex;

    public Exception getEx() {
        return ex;
    }

    public void setEx(Exception ex) {
        this.ex = ex;
    }

    public T getMessage() {
        return message;
    }

    public void setMessage(T message) {
        this.message = message;
    }
}


@Controller
public class MyControllerController {
    private Response<String> _response;
    private String message;

    public MyController() { _response = new Response<>(); }

 @RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody Response<String> signup(@RequestBody UserSignup details) {
        try{
            message = "";
             // code...
            _response.setMessage(message);
            return _response;
        }catch (Exception ex){
            _response.setEx(ex);
            return _response;
        }
    }
}

浏览器中的响应示例:

  

对象{message:&#34;&#34;,ex:null}