package com.java.example.demo.postman;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public
class
Constant {
@GetMapping(
"/first"
)
@ResponseBody
public
ResponseEntity<String> firstFunction(@RequestParam String param){
return
res(param +
"-init"
);
}
@GetMapping(
"/second"
)
@ResponseBody
public
ResponseEntity<String> secondFunction(@RequestParam String param){
return
res(
"加工后的随机数:"
+ param);
}
private
static
ResponseEntity<String> res(Object object){
Map<String,Object> map =
new
HashMap<>();
map.put(
"res"
,object);
return
jsonResult(JSONObject.toJSONString(map));
}
public
static
ResponseEntity jsonResult(String result) {
HttpHeaders resHeaders =
new
HttpHeaders();
resHeaders.set(
"Content-Type"
,
"application/json;charset=UTF-8"
);
return
new
ResponseEntity(result, resHeaders, HttpStatus.OK);
}
}