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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to use GET type also, in that case I will have to passit in URL
RequestParam always be string , convert that string to Json object in your method, using Jackson json parser.
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
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; }
}

1 Comment

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
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.