gpt4 book ai didi

json - 带有 Spring Boot REST Controller 和 JSON 的空字段

转载 作者:行者123 更新时间:2023-12-03 10:11:44 25 4
gpt4 key购买 nike

我有一个带有这个 Gradle 构建文件的 Spring Boot 应用程序:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'war'

sourceCompatibility = 1.7
targetCompatibility = 1.7

version = '1.0'

buildscript {
ext {
springBootVersion = '1.1.9.RELEASE'
}
repositories {
maven { url "http://repo.spring.io/libs-snapshot" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
}
}

jar {
manifest {
attributes 'Implementation-Title': 'Server', 'Implementation-Version': version
}
}

war {
baseName = 'gs-convert-jar-to-war'
version = '0.1.0'
}

repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-snapshot" }
maven { url "http://maven.springframework.org/milestone" }
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-tomcat:${springBootVersion}")

compile("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-aop:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-security:${springBootVersion}")
compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}")

compile("org.springframework.data:spring-data-rest-webmvc")

compile("org.hsqldb:hsqldb")

compile("com.google.guava:guava:17.0")
compile("org.apache.httpcomponents:httpclient:4.3.4")
compile("commons-io:commons-io:2.4")

testCompile("junit:junit")
}

使用此应用程序文件:

@EnableAutoConfiguration
@Configuration
@ComponentScan
@Import({ BasicSecurityConfiguration.class, HTTPSTomcatConfiguration.class,
MultiPartConfiguration.class })
public class Application extends RepositoryRestMvcConfiguration {

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

// use JSON
@Override
public ObjectMapper halObjectMapper() {
return new ResourcesMapper();
}

}

这个配置文件:

@Configuration
@EnableWebSecurity
public class BasicSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
// no cache
http.requestCache().requestCache(new NullRequestCache());

// use HTTPS
http.requiresChannel().anyRequest().requiresSecure();
http.portMapper().http(8080).mapsTo(8443);

// this replaces the web security http configuration
http.authorizeRequests().antMatchers("/**").authenticated().and()
.httpBasic();

// disable CSRF
http.csrf().disable();
}

@Autowired
protected void registerAuthentication(
final AuthenticationManagerBuilder auth) throws Exception {

// FIXME create a full user registry
// in-memory users
auth.inMemoryAuthentication().withUser("admin").password("pass")
.authorities("admin", "user").and().withUser("user")
.password("pass").authorities("user");
}

}

@Configuration
public class HTTPSTomcatConfiguration {
@Bean
EmbeddedServletContainerCustomizer containerCustomizer(
@Value("8443") final int port,
@Value("keystore.p12") Resource keystoreFile,
@Value("tomcat") final String alias,
@Value("localhost") final String keystorePass,
@Value("PKCS12") final String keystoreType) throws Exception {

final String absoluteKeystoreFile = keystoreFile.getFile()
.getAbsolutePath();

return new EmbeddedServletContainerCustomizer() {
public void customize(ConfigurableEmbeddedServletContainer container) {
TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
tomcat.addConnectorCustomizers(new TomcatConnectorCustomizer() {
public void customize(Connector connector) {
connector.setPort(port);
connector.setSecure(true);
connector.setScheme("https");
Http11NioProtocol proto = (Http11NioProtocol) connector
.getProtocolHandler();
proto.setSSLEnabled(true);
proto.setKeystoreFile(absoluteKeystoreFile);
proto.setKeyAlias(alias);
proto.setKeystorePass(keystorePass);
proto.setKeystoreType(keystoreType);
}
});
}
};
}
}

@Configuration
@EnableConfigurationProperties(MultipartProperties.class)
public class MultiPartConfiguration {

@Bean
public MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}

}

这个 Controller 接口(interface):

public interface Controller {

@RequestMapping(value = "/t", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Test t(@RequestBody Test test);

}

和这个 Controller 实现:

@RestController
public class ControllerImpl implements Controller {

@Override
public Test t(Test test) {
return test;
}
}

一切似乎都工作正常,除了从客户端到服务器的 JSON 序列化。当我执行 curl命令:
curl --insecure -H "Authorization: Basic YWRtaW46cGFzcw==" -H "Content-Type: application/json" -X POST -d '{"a":"a","b":"b"}' https://localhost:8443/t

我得到:
{"a":null,"b":null}

如果我只在 Controller 实现中进行调试,我会看到字段为 null .如果我像这样修改 Controller :

@Override
public Test t(Test test) {
Test t = new Test();
t.setA("a");
t.setB("b");
return t;
}

我得到正确答案:
{"a":"a","b":"b"}

为什么我的应用没有正确反序列化 Test 对象?

最佳答案

您的界面中有 @RequestBody 注释,您需要在方法实现中使用它:

@RestController 
public class ControllerImpl implements Controller {

@Override
public Test t(@RequestBody Test test) {
return test;
}
}

关于json - 带有 Spring Boot REST Controller 和 JSON 的空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com