Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅当在Spring引导控制器上运行Eclipse:POST失败并出现HTTP错误415时 问题是:_Java_Eclipse_Spring Boot_Http Status Code 415 - Fatal编程技术网

Java 仅当在Spring引导控制器上运行Eclipse:POST失败并出现HTTP错误415时 问题是:

Java 仅当在Spring引导控制器上运行Eclipse:POST失败并出现HTTP错误415时 问题是:,java,eclipse,spring-boot,http-status-code-415,Java,Eclipse,Spring Boot,Http Status Code 415,我们有一个工作的Spring Boot后端服务器(Java11,Spring Boot 2.2.4.RELEASE),带有React前端,如果通过Java-jar app.jar和IntelliJ运行,它在Docker容器中完美运行 如果它是通过Eclipse(在Windows Server 2016上运行)运行的,那么在尝试发送带有JSON正文的帖子时,会遇到以下错误: 2020-03-09 15:09:52.515 WARN 218960 --- [nio-8080-exec-1] .c.

我们有一个工作的Spring Boot后端服务器(Java11,Spring Boot 2.2.4.RELEASE),带有React前端,如果通过
Java-jar app.jar
IntelliJ运行,它Docker容器中完美运行

如果它是通过Eclipse(在Windows Server 2016上运行)运行的,那么在尝试发送带有JSON正文的帖子时,会遇到以下错误:

2020-03-09 15:09:52.515  WARN 218960 --- [nio-8080-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class xxx.xxx.xxx.xxx.xxx.xxx.xxx]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid type definition for type `xxx.xxx.xxx.xxx.xxx.xxx.xxx`: Argument #0 has no property name, is not Injectable: can not use as Creator [constructor for xxx.xxx.xxx.xxx.xxx.xxx.xxx, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}]
2020-03-09 15:09:52.517  WARN 218960 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
此错误导致通过前端代码设置的标题的重新命名。因此,通过设置内容类型:application/json,同样的错误也会发生(请参见下面的标题)

有没有人遇到过类似的事情,只有在Eclipse中才会发生?

我已经检查了编码(在Eclipse中将所有内容设置为UTF-8),并确保所有环境在IntelliJ和Eclipse中使用相同的Java版本(我使用的是通过SDKMan安装的AdoptOpenJDK 11.0.6.hs-adpt)

编译的类文件看起来完全相同。运行应用程序时的类路径包含相同的jackson库(总体上看起来非常相似)

我相信这与Eclipse如何运行应用程序有关。有人知道为什么会观察到这种奇怪的行为吗?为什么只有Eclipse?既然我们有一些开发人员使用Eclipse,我们如何解决它呢?


有关请求的更多信息: 通过浏览器查看的请求标题:

POST /server/configure HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 510
Accept: application/json, text/plain, */*
Sec-Fetch-Dest: empty
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36
Content-Type: application/json
Origin: http://localhost:3000
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,de-DE;q=0.8,de;q=0.7
答复如下:

{"timestamp":"2020-03-09T14:00:27.285+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/json;charset=UTF-8' not supported","path":"/server/configure"}
和响应标题:

HTTP/1.1 415
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: http://localhost:3000
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 09 Mar 2020 13:53:05 GMT
Keep-Alive: timeout=60
Connection: keep-alive

有关该应用程序的更多信息: 控制器如下(简化):

ConnectionConfiguration类如下所示(此处显示的属性较少):

关于项目结构,我们有一个根文件夹,其中包含一个后端和一个前端文件夹。每个(根、后端和前端)都包含一个build.gradle文件,通过根文件夹中的build.gradle链接在一起。通过这样做,我们可以构建一个生产构建,构建前端,将静态HTML和JavaScript文件复制到后端的resources/static文件夹,然后构建Spring引导jar。然后,在生产模式下,前端通过Spring Boot的Tomcat交付

对于开发,我们通常通过
npm run start
为前端启动一个Webpack dev服务器,并从Application.java类中启动Spring Boot应用程序,该类通过IDE包含一个主方法:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

在拥有相等的类路径之后,我们检查生成的.class文件是否真的是相同的-它们不是(请参见下面的)。我们意识到,在Eclipse的Preferences/Java/Compiler中设置了“存储关于方法参数的信息(可通过反射使用)”选项之后,Everythingg在通过Eclipse运行后端时也工作得很好:

知道了这一点,反序列化为什么不起作用是有道理的。Jackson依赖于构造函数中用
@JsonCreator
注释的参数名来实现这一点,它通过反射来获取名称

另见:


另一种解决方案可能更安全,因为您不必编辑IDE的设置: 如果我们用
@JsonProperty(“name”)
注释参数,那么它也可以在没有
-parameters
编译选项的情况下工作:

package xxx.xxx.xxx.xxx.xxx.xxx;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ConnectionConfiguration {

    private String someProperty;

    @JsonCreator
    public ConnectionConfiguration(@JsonProperty("someProperty") String someProperty) {
        this.someProperty= someProperty;
    }

    public String getSomeProperty() {
        return someProperty;
    }

}

字节码部分(不带
@JsonProperty
注释): 使用
-参数构建

...
  // access flags 0x1
  // signature (Ljava/lang/String;)V
  // declaration: java.lang.String)
  public <init>(Ljava/lang/String;)V
    // parameter  someProperty
  @Lcom/fasterxml/jackson/annotation/JsonCreator;()
   L0
    LINENUMBER 17 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
...
...
  // access flags 0x1
  // signature (Ljava/lang/String;)V
  // declaration: java.lang.String)
  public <init>(Ljava/lang/String;)V
  @Lcom/fasterxml/jackson/annotation/JsonCreator;()
   L0
    LINENUMBER 16 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
...
...
  // access flags 0x1
  // signature (Ljava/lang/String;)V
  // declaration: java.lang.String)
  public <init>(Ljava/lang/String;)V
    // parameter  someProperty
  @Lcom/fasterxml/jackson/annotation/JsonCreator;()
   L0
    LINENUMBER 17 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
...
...
  // access flags 0x1
  // signature (Ljava/lang/String;)V
  // declaration: java.lang.String)
  public <init>(Ljava/lang/String;)V
  @Lcom/fasterxml/jackson/annotation/JsonCreator;()
   L0
    LINENUMBER 16 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
...