ResponseEntity 不 return json 响应 Post 方法
ResponseEntity do not return json response for Post Method
我在发送 post 方法时很难获得 JSON 格式的响应。
我的控制器和响应 class 如下。此外,我在 pom.xml
中使用了 Jackson 依赖项,并且我使用 @RestController
作为控制器注释。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<version>2.8.0</version>
</dependency>
我希望响应为 {Avalue:a, Bvalue:b} 但响应为 returns null。
你能帮我找到我失踪的地方吗?
@RestController
public class Controller{
private PostService postService;
@RequestMapping(value = "/create", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<PostResponse> create(@RequestBody VInfo v) {
VInfo created = postService.createVInfo(v);
PostResponse pr = new PostResponse();
if (created == null) {
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
} else {
pr.a_value= v.a_value;
pr.b_value= v.b_value;
return new ResponseEntity<PostResponse>(pr,HttpStatus.OK);
}
}
}
public class PostResponse {
@JsonProperty("Avalue")
public String A_VALUE;
@JsonProperty("Bvalue")
public String B_VALUE;
}
@Service
public class PostService {
@Autowired
private CreateVRepository postRepository;
public VInfo createVInfo(VInfo vInfo){
VInfo v1= new VInfo ();
v1.setA_VALUE(vInfo.getA_VALUE());
v1.setB_VALUE(vInfo.getB_VALUE());
postRepository.save(v1);
return v1;
}
}
我在我的控制器上使用了记录器,我可以看到记录器毫无问题地传递给其他括号。当我记录我的 pr.a 和 pr.b 对象时,它们 returns 期望值。但是响应仍然 returns 为空。
您将需要 getter/setter 用于 PostResponse class
尝试在 class 上添加注释 @RestController
。
下面有getters和setters吗class?
public class PostResponse {
@JsonProperty("Avalue")
public String A_VALUE;
@JsonProperty("Bvalue")
public String B_VALUE;}
您在 class 中将 A_VALUE 和 B_VALUE 作为属性,而您正在设置 a_value 和 b_value 的值.
自动装配您的 PostService class 并在您已经具有 starter-web 依赖项时删除这些(Jackson)依赖项。我还建议您在 class 级别使用 Lombok 的 @Data 和 @NoArgsConstructor 注释。
我在发送 post 方法时很难获得 JSON 格式的响应。
我的控制器和响应 class 如下。此外,我在 pom.xml
中使用了 Jackson 依赖项,并且我使用 @RestController
作为控制器注释。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<version>2.8.0</version>
</dependency>
我希望响应为 {Avalue:a, Bvalue:b} 但响应为 returns null。 你能帮我找到我失踪的地方吗?
@RestController
public class Controller{
private PostService postService;
@RequestMapping(value = "/create", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<PostResponse> create(@RequestBody VInfo v) {
VInfo created = postService.createVInfo(v);
PostResponse pr = new PostResponse();
if (created == null) {
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
} else {
pr.a_value= v.a_value;
pr.b_value= v.b_value;
return new ResponseEntity<PostResponse>(pr,HttpStatus.OK);
}
}
}
public class PostResponse {
@JsonProperty("Avalue")
public String A_VALUE;
@JsonProperty("Bvalue")
public String B_VALUE;
}
@Service
public class PostService {
@Autowired
private CreateVRepository postRepository;
public VInfo createVInfo(VInfo vInfo){
VInfo v1= new VInfo ();
v1.setA_VALUE(vInfo.getA_VALUE());
v1.setB_VALUE(vInfo.getB_VALUE());
postRepository.save(v1);
return v1;
}
}
我在我的控制器上使用了记录器,我可以看到记录器毫无问题地传递给其他括号。当我记录我的 pr.a 和 pr.b 对象时,它们 returns 期望值。但是响应仍然 returns 为空。
您将需要 getter/setter 用于 PostResponse class
尝试在 class 上添加注释 @RestController
。
下面有getters和setters吗class?
public class PostResponse {
@JsonProperty("Avalue")
public String A_VALUE;
@JsonProperty("Bvalue")
public String B_VALUE;}
您在 class 中将 A_VALUE 和 B_VALUE 作为属性,而您正在设置 a_value 和 b_value 的值.
自动装配您的 PostService class 并在您已经具有 starter-web 依赖项时删除这些(Jackson)依赖项。我还建议您在 class 级别使用 Lombok 的 @Data 和 @NoArgsConstructor 注释。