0

I am using spring boot, and while making a restcontroller or controller if I use the jsonobject type request then it doesnt work, whereas same works when I change type to string.

@Controller
@RequestMapping("rest/dummy")

public class CustomerController {

    @GetMapping("test")
    public ResponseEntity test(@RequestParam("req") JSONObject inputData) {
        org.json.JSONObject response = new org.json.JSONObject();
        response.put("abc", "123");
        return new ResponseEntity(inputData.toString(), HttpStatus.OK);
    }

pom.xml:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.8.RELEASE</version>
</dependency>
<dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20171018</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

I do want to use it both GET and POST type and also I want to use jsonobject for both request and response as the data can change on fly and its type.

kapil gupta
  • 335
  • 3
  • 19
  • Possible duplicate of [Returning JSON object as response in Spring Boot](https://stackoverflow.com/questions/44839753/returning-json-object-as-response-in-spring-boot) – utpal416 Nov 28 '17 at 19:01

3 Answers3

2

In RequestParam , we send key values which added in URL, To send Json object send it in RequestBody .

Use @RequestBody and send your Json in body part of your request.

Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44
  • I want to use GET type also, in that case I will have to passit in URL – kapil gupta Nov 28 '17 at 19:26
  • RequestParam always be string , convert that string to Json object in your method, using Jackson json parser. – Ravat Tailor Nov 29 '17 at 02:14
  • No it is not working, but what I noticed is same application works fine on linux whereas on windows it doesnt. In Linux it works with RequestParam as JSONObject – kapil gupta Nov 30 '17 at 11:24
0

Using real POJOs as params and return values is the better approach imo. Use Jackson annotations to configure those POJOs.

Anyways. This should work:

@GetMapping("test")
public ResponseEntity<String> test(@RequestParam("req") JSONObject inputData) {
    org.json.JSONObject response = new org.json.JSONObject();
    response.put("abc", "123");
    return ResponseEntity.ok(inputData.toString());
}

alternatively

@GetMapping("test")
public ResponseEntity<SomeOutputDto> test(@RequestParam("req") String inputData) {

    SomeOutputDto out = new SomeOutputDto();
    out.setAbc(123);
    return ResponseEntity.ok(dto);
}

this requires a additional class: SomeOutputDto, but on the other hand, you have more control over your code.

public class SomeOutputDto {
   private int abc = 0;

  public void setAbc(int v) {
    this.abc = v;
  }
  public int getAbc() { return this.abc; }
}
helt
  • 4,925
  • 3
  • 35
  • 54
  • Tried but it didnt work, also I want to use json as I have request which can keep changing and do not have rigid structure as of POJO – kapil gupta Nov 28 '17 at 19:28
0

Got it working by using apache-tomcat 8.0.15, the same doesnt work with apache-tomcat 8.0.49

kapil gupta
  • 335
  • 3
  • 19