1

I have a Tomcat app which has RESTService. @Get works just fine and responds proper json objects. Problem is when trying to implement @Post json service, I am always getting bad request error 400.

After all, my purpouse is to upload and object of type Program

public class Program {

private long start;
private long stop;
private double temperature;

public Program(long start, long stop, double temperature) {
    this.start = start;
    this.stop = stop;
    this.temperature = temperature;
}
}

This is the json I want to upload :

{
   "stop":1486119421283,
   "start":1486119421283,
   "temperature":2
}

Here is my code:

@POST
@Path("/program")
@Consumes(MediaType.APPLICATION_JSON)
public Response crunchifyREST(InputStream incomingData) {
    StringBuilder crunchifyBuilder = new StringBuilder();
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
        String line = null;
        while ((line = in.readLine()) != null) {
            crunchifyBuilder.append(line);
        }
    } catch (Exception e) {
        logger.error("Error Parsing: - ");
    }
    logger.debug("Data Received: " + crunchifyBuilder.toString());

    // return HTTP response 200 in case of success
    return Response.status(200).entity(crunchifyBuilder.toString()).build();
}

and these are maven dependencies

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>home</groupId>
<artifactId>CTemp</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<name>CTemp</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.7</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.7</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.7</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>

    <dependency>
        <groupId>com.pi4j</groupId>
        <artifactId>pi4j-core</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160212</version>
        <type>jar</type>
    </dependency>

    <!-->      REST DEPENDENCIES   -->

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.hk2.external</groupId>
        <artifactId>asm-all-repackaged</artifactId>
        <version>2.2.0-b14</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.25</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.25</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.25</version>
    </dependency>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.4</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.25</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>CTemp</finalName>
</build>

e2rabi
  • 4,728
  • 9
  • 42
  • 69
Toni
  • 165
  • 1
  • 13
  • You are sending json object and expecting input stream, so bound to get error. Just change input to JsonNode/Program Object eg: crunchifyREST(Program program) or crunchifyREST(JsonNode data) – user1211 Feb 03 '17 at 11:02
  • I've changed it but same happens – Toni Feb 03 '17 at 11:23
  • Json Values should be in this format { "stop":"1486119421283", "start":"1486119421283", "temperature":"2" } – AnilCk Feb 03 '17 at 13:35
  • 1
    How are you invoking the REST service? Are you sure you're setting the `Content-type` header to `application/json`? If you can recreate the issue in a text-only HTTP client tool (e.g. curl) and show us the command and response, that would help. – slim Feb 03 '17 at 16:25
  • See: http://stackoverflow.com/a/7173011/7512 – slim Feb 03 '17 at 16:30

2 Answers2

0

Your code works fine. (It has some minor flaws, which you'll see if you pay attention to compiler/IDE warnings). I copied it into a Tomcat/Jackson project and tried it out:

$ curl -H "Content-Type: application/json" \
     -X POST \
     -d '{ "stop":1486119421283, "start":1486119421283, "temperature":2 }' \
     http://localhost:8080/myproject/program
{ "stop":1486119421283, "start":1486119421283, "temperature":2 }

You are probably sending a bad request (just like the error message tells you). We can't guess what that would be without seeing how you send the request.

I tried sending an invalid content-type, and got a 415, not a 400:

$ curl -f -H "Content-Type: text/plain" \
     -X POST \
     -d '{ "stop":1486119421283, "start":1486119421283, "temperature":2 }' \
     http://localhost:8080/myproject/program
curl: (22) The requested URL returned error: 415 Unsupported Media Type

Note that because you're not actually doing any JSON processing, it's irrelevant at this stage whether the body is actually JSON:

$ curl -H "Content-Type: application/json" \
     -X POST \
     -d 'Not actually JSON' \
     http://localhost:8080/myproject/program
Not actually JSON
slim
  • 40,215
  • 13
  • 94
  • 127
-4

The request was malformed, so you got 400 error. You need the double quotes in the values. Jason works with strings, so just change your code. Below an example :

{
  "one": "1:1",
  "two": {
    "three": "3:3"
  }
}
  • Numbers in JSON don't need quotes: `{ "anInteger": 42, "aFloat": 2.4 }` is valid JSON. – slim Feb 03 '17 at 16:22
  • Also Toni's program does nothing JSON-y with the input, so malformed JSON wouldn't cause a 400 error. – slim Feb 03 '17 at 16:27