无法将String接收到Spring终点

时间:2019-04-20 22:01:28

标签: java spring spring-boot spring-mvc

我使用以下代码从POSTMAN接收值作为键值:

@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, value = "/v1/notification")
  public ResponseEntity<String> handleNotifications(@RequestBody MultiValueMap<String, Object> keyValuePairs) {

    LOGGER.debug("handleFormMessage");
    LOGGER.debug("keyValuePairs: {}", keyValuePairs);

    String unique_id = String.valueOf(keyValuePairs.get("uniqueid"));
    String status = String.valueOf(keyValuePairs.get("status"));
    String external_id = String.valueOf(keyValuePairs.get("external_id"));
    String gateway = String.valueOf(keyValuePairs.get("gateway"));
    String transaction_type = String.valueOf(keyValuePairs.get("transaction_type"));

    try {

      System.out.println("!!!!!");
      System.out.println("!!!! unique_id " + unique_id);

          System.out.println("!!!!! Entered if(paymentTransaction.isPresent()) {");

    } catch (Exception e) {
      e.printStackTrace();
      return new ResponseEntity<>("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR);
    }

    return new ResponseEntity<>(HttpStatus.OK);
  }

我从POSTMAN发送这些值:

type=sale&
uniqueid=28357oxojqmehujo8auz9v27p0zrh1nl&
transaction_type=sale3d&
status=approved&
external_id=43435&
amount=500&
gateway=ipg&
signature=088e16a1019277b15d58faf0541e11910eb756f6&

我在日志中得到了

00:56:25,691 INFO  [stdout] (default task-1) 00:56:25.691 [default task-1] DEBUG o.d.r.a.s.controller.HomeController - handleFormMessage
00:56:25,692 INFO  [stdout] (default task-1) 00:56:25.692 [default task-1] DEBUG o.d.r.a.s.controller.HomeController - keyValuePairs: {
00:56:25,692 INFO  [stdout] (default task-1) status=[approved], 
00:56:25,692 INFO  [stdout] (default task-1) external_id=[43435], 
00:56:25,692 INFO  [stdout] (default task-1) signature=[088e16a1019277b15d58faf0541e11910eb756f6], 
00:56:25,692 INFO  [stdout] (default task-1) uniqueid=[28357oxojqmehujo8auz9v27p0zrh1nl], 
00:56:25,692 INFO  [stdout] (default task-1) amount=[500], 
00:56:25,692 INFO  [stdout] (default task-1) =[], type=[sale], 
00:56:25,692 INFO  [stdout] (default task-1) gateway=[ipg], 
00:56:25,692 INFO  [stdout] (default task-1) transaction_type=[sale3d]}
00:56:25,693 INFO  [stdout] (default task-1) !!!!!
00:56:25,696 INFO  [stdout] (default task-1) !!!! unique_id null

您可以确定已接收到值,但字符串unique_id为空。您知道为什么这些值为空吗?

应用程序配置:

public class Application extends SpringBootServletInitializer implements WebMvcConfigurer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(converter -> converter instanceof MappingJackson2XmlHttpMessageConverter);
        converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2XmlHttpMessageConverter(
                ((XmlMapper) createObjectMapper(Jackson2ObjectMapperBuilder.xml()))
                        .enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)));
        converters.add(new MappingJackson2HttpMessageConverter(createObjectMapper(Jackson2ObjectMapperBuilder.json())));
    }
    .....
}

POSTMAN配置:

enter image description here

您知道如何解决此问题吗?

1 个答案:

答案 0 :(得分:-1)

MultiValueMap允许每个键使用多个值,这意味着每个键都引用一个List<Object>。除非您的业务案例必须支持多个值,否则请尝试将RequestBody类型更改为Map<String, String>。然后您的代码简化为:

@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, value = "/v1/notification")
public ResponseEntity<String> handleNotifications(@RequestBody Map<String, String> keyValuePairs) {
    String unique_id = keyValuePairs.get("uniqueid");
    String status = keyValuePairs.get("status");
    String external_id = keyValuePairs.get("external_id");
    String gateway = keyValuePairs.get("gateway");
    String transaction_type = keyValuePairs.get("transaction_type");
    ...
}