使用Tomcat在json POST上的错误请求

时间:2017-02-03 10:59:22

标签: java json rest tomcat jersey

我有一个拥有RESTService的Tomcat应用程序。 @Get工作得很好,并响应适当的json对象。问题是当试图实现@Post json服务时,我总是得到错误的请求错误400。

毕竟,我的purpouse是上传类型为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;
}
}

这是我要上传的json:

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

这是我的代码:

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

这些是maven依赖

<?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>

2 个答案:

答案 0 :(得分:0)

您的代码运行正常。 (它有一些小缺陷,如果你注意编译器/ IDE警告,你会看到)。我将它复制到Tomcat / Jackson项目中并尝试了它:

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

您可能正在发送错误请求(就像错误消息告诉您的那样)。如果不了解您如何发送请求,我们无法猜测会是什么。

我尝试发送无效的内容类型,并获得了415,而不是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

请注意,因为您实际上没有进行任何JSON处理,所以在这个阶段,主体是否实际上是JSON是无关紧要的:

$ curl -H "Content-Type: application/json" \
     -X POST \
     -d 'Not actually JSON' \
     http://localhost:8080/myproject/program
Not actually JSON

答案 1 :(得分:-3)

请求格式错误,因此您遇到400错误。 您需要值中的双引号。 Jason使用字符串,所以只需更改代码即可。 下面是一个例子:

{
  "one": "1:1",
  "two": {
    "three": "3:3"
  }
}
相关问题