在使用香草Spring Boot 2 / Spring 5 / Java 10 / Jigsaw的新设置中,我遇到了一种奇怪的情况,无论我做什么,通过Spring MVC拉一个对象都会给我一个空的{} JSON对象,而不是我的对象属性。
但是...如果我使用application / xml的Accept标头而不是application / json,我将获得所有正确的属性。也许我不介意,但我似乎在以前的版本中回想过,如果它在一侧(xml)上有效,则应该在另一侧(json)上有效,反之亦然。
我已经在内部追溯到为我的模型类创建的BeanSerializer,没有属性。我只是不确定为什么会这样。我在执行过程中进行了跟踪,以查看Jackson在HTTP转换过程中是否在运行...它只是忽略了对象内部的所有属性。
这是我的设置:
行家:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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>10</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.9.6</version>
<scope>runtime</scope>
</dependency>
module-info:
module stupid.example {
opens com.example.microservice.datasynchronizer;
opens com.example.microservice.datasynchronizer.model;
opens com.example.microservice.datasynchronizer.webflux to spring.beans, spring.core, spring.web ;
opens com.example.microservice.datasynchronizer.dao to spring.core ;
requires java.base ;
requires java.xml.bind ;
requires spring.boot;
requires spring.boot.autoconfigure;
requires spring.beans ;
requires spring.context ;
requires spring.core ;
requires spring.data.commons ;
requires spring.web ;
requires spring.webmvc ;
requires java.persistence ;
requires org.junit.jupiter.api;
requires spring.test;
requires spring.boot.test ;
}
模型类(最新的类,以防万一,带有jaxb注释):
@Entity
@XmlRootElement
public class Thingamajig {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlElement
private Long id;
@XmlElement
private String firstName;
@XmlElement
private String lastName;
public Thingamajig ( ) { ; }
public Thingamajig(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format("Thingamajig [id=%d, firstName='%s', lastName='%s']", id, firstName, lastName);
}
}
控制器:
@RestController
public class ThingamajigController {
@Autowired
private ThingamajigDao _dao ;
@GetMapping("/thing/{id}")
public Thingamajig getPerson(@PathVariable Long id) {
Optional<Thingamajig> found = _dao.findById(id) ;
return found.get() ;
}
@PostMapping ( "/thing" )
@ResponseStatus(HttpStatus.CREATED)
public void add(@RequestBody Thingamajig person) {
_dao.save(person) ;
}
}
配置:
@EnableWebMvc
@SpringBootApplication
public class DataSynchronizerApplication {
public static void main(String[] args) throws Throwable {
SpringApplication.run(DataSynchronizerApplication.class, args);
}
}
我到底想念什么?任何帮助表示赞赏。
答案 0 :(得分:1)
您只是忘了在Thingamajig类中定义吸气剂和吸气剂。
XML之所以有效,是因为您已经在属性上定义了注释,但是JSON序列化程序正在寻找吸气剂。