Return mocked up JSON from file (JSONObject) in Spring Controller

11,807

Solution 1

This is what worked for me.

Java:

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;
    }

}

Kotlin:

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)
    }

}

Solution 2

@Controller
public class TestController {

    @RequestMapping(
      value = "/test", 
      method = RequestMethod.GET, 
      produces = MediaType.APPLICATION_JSON_VALUE
    )

    String getTest() {
        return "json/test.json";
    }
}

This worked for me.

Path to JSON file: \src\main\resources\static\json\test.json

Share:
11,807
Namek
Author by

Namek

Updated on July 11, 2022

Comments

  • Namek
    Namek almost 2 years

    I'd like to mock up some JSON (that I'm reading from a file), and return it as a result of some Spring Controller.

    File contains of course correct JSON data format inside, like:

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

    My controller looks like:

    @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;
        }
    }
    

    There is no issue with reading the file itself etc. jsonObject itself, is correct from debbuging PoV, however I'm getting HTTP Status 406 from the browser. I've tried also just returning String (by returning jsonObject.toString()), instead of JSONObject. However it causes encoding issue - so that JSON from the browser, is not the JSON itself (some additional slashes, quotation marks etc.).

    Is there any way, to return JSON from file?