从 Spring 控制器中的文件 (JSONObject) 返回模拟的 JSON



我想模拟一些JSON(我正在从文件中读取),并将其作为某些Spring Controller的结果返回。

文件中当然包含正确的JSON数据格式,例如:

{"country":"","city":""...}

我的控制器如下所示:

@RestController
@RequestMapping("/test")
public class TestController {
    @Value("classpath:/META-INF/json/test.json")
    private Resource testMockup;
    @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JSONObject getTest() throws IOException {
        JSONObject jsonObject = new JSONObject(FileUtils.readFileToString(testMockup.getFile(), CharEncoding.UTF_8));
        return jsonObject;
    }
}

读取文件本身等没有问题。 jsonObject本身,从 PoV 解扰中是正确的,但是我从浏览器获得 HTTP 状态 406。我也尝试只返回字符串(通过返回jsonObject.toString()),而不是JSONObject.但是它会导致编码问题 - 因此来自浏览器的JSON不是JSON本身(一些额外的斜杠,引号等)。

有什么方法可以从文件返回 JSON?

这就是对我有用的。

爪哇岛:

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
@RestController("/beers")
public class BeersController {
    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody
    Object getBeers() {
        Resource resource = new ClassPathResource("/static/json/beers.json");
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(resource.getInputStream(), Object.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

科特林:

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.core.io.ClassPathResource
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/beers")
class BeersController {
    @GetMapping
    fun getBeers(): Any? {
        val resource = ClassPathResource("/static/json/beers.json")
        return ObjectMapper().readValue(resource.inputStream, Any::class.java)
    }
}
@Controller
public class TestController {
    @RequestMapping(
      value = "/test", 
      method = RequestMethod.GET, 
      produces = MediaType.APPLICATION_JSON_VALUE
    )
    String getTest() {
        return "json/test.json";
    }
}

这对我有用。

JSON 文件的路径:srcmainresourcesstaticjsontest.json

这不是有效的 JSON。如果不是拼写错误,请尝试重新格式化文件,使其看起来像

{"country":"","city":""}

请注意属性名称周围的开场引号。

你试过杰克逊吗?

ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(input, Object.class);
String s = mapper.writeValueAsString(json);

也许直接写到响应体?杰克逊应该照顾好json。

我知道

我已经很晚了,但你为什么不尝试以下解决方法。

@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String getTest() throws IOException {
    JSONObject jsonObject = new JSONObject(FileUtils.readFileToString(testMockup.getFile(), CharEncoding.UTF_8));
    return jsonObject.toString();
}

最新更新