java - 使用 Tomcat 对 json POST 的错误请求

标签 java json rest tomcat jersey

我有一个具有 RESTService 的 Tomcat 应用程序。 @Get 工作正常并响应正确的 json 对象。问题是在尝试实现 @Post json 服务时,我总是收到错误请求错误 400。

毕竟我的目的是上传和对象类型的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>

最佳答案

您的代码运行良好。 (它有一些小缺陷,如果您注意编译器/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

关于java - 使用 Tomcat 对 json POST 的错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42022664/

相关文章:

Java:不区分大小写的枚举 Jersey 查询参数绑定(bind)

ruby-on-rails - "render :nothing => true"返回空的纯文本文件?

javascript - 将 PUT 方法与 ajax Knockout js 一起使用

java - 如何使用 MBean 上传文件?

java - java中如何判断一个对象是字符串还是字符串数组?

java - xsd 未在某些系统上验证 : "Cannot resolve the name to a(n) ' type definition' component.“

java - Jersey JSON 和日期

json - Jmeter:解析 BeanShell Prepocessor 中的 JSON 响应并将其存储在变量中

java - 用于 http 和 https 的 Jax-rsClient 程序

javascript - 如何使用 '+' 连接 JSON 数据中的字符串?