I'm create REST api with springFramework. Create products from post api, but rest method does not cast the parameters with TitleCase parameter name.
My Product model:
public class Product extends BaseModel {
private String title;
private String shortDescription;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
}
My Product REST Api:
@PostMapping(value = "/product", consumes = {MediaType.APPLICATION_JSON_VALUE})
public ApiResponse Insert(@RequestBody Product model){
ApiResponse response = new ApiResponse();
try{
response.setData(_service.Insert(model));
response.setSuccess(true);
} catch (Exception ex){
response = _service.HandleException(ex);
}
return response;
}
Worked Request Object:
{
"title" : "TEST",
"shortDescription" : "SD",
"longDescription" : "LD"
}
Not Worked Request Object:
{
"Title" : "TEST",
"ShortDescription" : "SD",
"LongDescription" : "LD"
}
I want both options to work. I'm new to Java, so I did not know how to get around to it, so I wanted to ask.
UPDATED
Jackson Config File
@Configuration
@EnableWebMvc
public class JacksonConfiguration {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
return mapper;
}
}
Thanks.
This works fine:
package com.example;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class Test {
private ObjectMapper mapper;
public static class A{
private int test;
public int getTest() {
return test;
}
public void setTest(int test) {
this.test = test;
}
}
@Before
public void setUp(){
mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
}
@Test
public void deserialise() throws Exception {
String json = "{\"test\":\"2\"}";
assertThat(mapper.readValue(json, A.class).getTest(), is(2));
}
@Test
public void deserialiseIgnoringCase() throws Exception {
String json = "{\"Test\":\"2\"}";
assertThat(mapper.readValue(json, A.class).getTest(), is(2));
}
}
Product
model does not seem to have longDescription
field. That might be the reason why deserialisation will fail (as there is no @JsonIgnore
) on Product
class. Below snippet works fine:
class Product {
private String title;
private String shortDescription;
private String longDescription;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getLongDescription() {
return longDescription;
}
public void setLongDescription(String longDescription) {
this.longDescription = longDescription;
}
}
public static void main(String[] args) throws Exception{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Product product = mapper.readValue("{\"Title\" : \"TEST\",\"ShortDescription\" : \"SD\",\"LongDescription\" : \"LD\"}", Product.class);
System.out.println(product.getShortDescription());
}
It will throw an Exception
if you comment out longDescription
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.