JAVA API编写

前言

日前,接到任务写一个 API,思考了一番,决定用 JAVA 语言编写,配合 spring boot 框架,先初步实现了一个本地调用 API ,具体代码如下。

代码

package com.example.demo.controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject; 
@RestController
public class TestController {
	@ResponseBody
	@RequestMapping(value="/json/data",method=RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String hello(@RequestBody JSONObject jsonParam) {
		System.out.println(jsonParam.toJSONString());
		JSONObject json=new JSONObject();
		json.put("code", "success");
		json.put("message", "hello world");
		return json.toString();
	}
}

需要注意的地方有以下两点:

  1. JSONObject类最好选用 Maven 中 jar 包,否则,不保证对。
  2. produces = “application/json;charset=UTF-8” 这句话记得写,否则可能会出现请求头错误。

结束语

本人大三学生一枚,学识尚浅,不喜勿喷,希望今日能抛砖引玉,请各位大佬一定不吝赐教!!!

你可能感兴趣的:(json,java)