2

I'm having troubles with returning a JSONObject in Java Spring. I've been googling fanaticly but haven't found a solution yet.

I'm using Jackson's fasterxml but I still can't get my API to respond in JSON. I'll include the files which I think are necessary to help.

First off I'll show my pom.xml, in which I think the error origins. There might be a collision between Jackson and another dependency, but I can't seem to figure out which one.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF- 
   8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.vaadin.external.google</groupId>
        <artifactId>android-json</artifactId>
        <version>0.0.20131108.vaadin1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

The controller on which the error occurs goes as following. You should be looking at testMeth2()

@RestController
@RequestMapping(path="/test")
@EnableWebMvc
public class TestController {

@Autowired
private CourseRecommendation courseRecommendation;
@Autowired
private EmployeesRepository employeesRepository;

Employees emp;


@RequestMapping(value = "/check/1", method = RequestMethod.GET)
public @ResponseBody
Employees testMeth1() {
    emp = employeesRepository.findById(1);

    emp.setSex("F");
    employeesRepository.save(emp);

    return emp;
};

@RequestMapping(value = "/check/2", method = RequestMethod.GET)
public @ResponseBody
JSONObject testMeth2() {

    return new JSONObject();

}
}

I will include the stacktrace aswell on pastebin:

https://pastebin.com/5BsEk9C7

This is in a project I'm doing for an internship and every bit help would be greatly appreciated!

Cheers.

J. Doe
  • 43
  • 5
  • Is there a reason you put the Jackson version number in rather than inheriting it from spring-boot-parent? May be messing with the AutoConfig of Jackson – mep Mar 26 '18 at 18:45
  • test Meth too (lolz) - I'd rather not.. :) – Chetan Jadhav CD Mar 26 '18 at 18:54
  • @FattySalami I have tried both, I'll change it though :) – J. Doe Mar 26 '18 at 19:00
  • From what I’ve looked at it looks like JSONObject class doesn’t have getters for some fields and so Jackson actually cannot convert it. https://stackoverflow.com/a/44842806/7770917 – mep Mar 26 '18 at 19:06

1 Answers1

0

I ended up just using a POJO, which Spring converted to a JSON for me. It's not the same, but its a good workaround for my project.

Cheers!

J. Doe
  • 43
  • 5