user2405589
user2405589

Reputation: 968

RequestBody JSON format has single quotes

I have a controller class in my SpringBoot app.

@RestController
@RequestMapping("/{database}/alerts")
public class ControllerB {
  @Autowired private ServiceB serviceB;

  @Autowired
  private B b;

  @Autowired
  ControllerB(B b,ServiceB serviceB){
      this.b = b;
      this.serviceB = serviceB;
  }

  @RequestMapping(method = RequestMethod.POST)
  public B dosomethingCrazy(@RequestBody BImpl bimpl)  {


      String response = serviceB.dosomethingImportant();
      return bimpl;

  }

}

The problem arises when the requestBody has a single quote in the value. For example if the requestbody is {"name" : "peter o'toole"}, it throws a http 400

"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"JSON parse error: Unexpected end-of-input in VALUE_STRING

Any idea how Jackson can map a single quote value to the object field?

Upvotes: 5

Views: 4011

Answers (3)

Arnab Gupta
Arnab Gupta

Reputation: 717

Not sure what version of Spring Boot you are using. With 1.4.3.RELEASE, I can curl just fine like this to my endpoint:

curl -X POST --header "Content-Type: application/json" --header "Accept: */*" -d "{\"name\":\"peter o'toole\"}" "https://localhost:5443/test"

I should add though that our Jackson version is 2.7.2. I am not sure if that causes it to work.

Upvotes: 0

Wim Deblauwe
Wim Deblauwe

Reputation: 26858

Set the following property in your application.properties file:

spring.jackson.parser.allow-single-quotes=true

Upvotes: 8

Dherik
Dherik

Reputation: 19070

Create this class to setup the Jackson and enable Single quotes:

@Configuration
public class JsonConfigurer {

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.build();
        objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
        return objectMapper;
    }

}

Upvotes: 5

Related Questions