Spring-将Pojo转换为JSON时出现问题(未找到转换器错误)

时间:2018-09-16 11:38:51

标签: java spring hybris

春季4.3.3

我正在尝试将Pojo转换为JSON,将Controller标记为
@RestController,问题在于某些元素的首字母小写而不是大写,

Ex:
"Id": 1, //This is ok  
"customerId": "1234", //Instead of CustomerId, this has customerId
...

控制器

@RestController
...
public class CustomerController{
    ...
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public CustomerResponse postCustomerRequest(final HttpServletRequest request) { 

我希望大写。 pojo基本上是xsd的xjc生成的类,其中包含

@XmlElement(name = "Id")
protected int id;
@XmlElement(name = "CustomerId")
protected String customerId;
...
    public int getId() {
        return id;
    }
    public void setId(int value) {
        this.id = value;
    }
    public String getCustomerId() {
        return customerId;
    }
    public void setCustomerId(String value) {
        this.customerId = value;
    }

这与每个属性都有关联的setter和getter。在Controller中,我也将不区分大小写的ObjectMapper设置为true,

mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); 

我也尝试过,将Controller标记为@Controller而不是@RestController,在方法之前提供@ResponseBody,

控制器

@Controller
...
public class CustomerController {
...
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @ResponseBody
    public String postCustomerRequest(HttpServletRequest request) {
        ...

//Used PropertyNamingStrategy with the ObjectMapper, converted the first character to an upper case, 
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
            ...
            CustomerResponse response=createCustomer(document,objectFactory);
            mapperObj.setPropertyNamingStrategy(new CustomerJsonNameStrategy());
            String jsonOutput = mapperObj.writeValueAsString(response);
            return jsonOutput;

如果我在Eclipse调试期间看到jsonOutput的值,则它以正确的大小写输出json元素,但是对其余客户端的响应是这样的,

{"errors": [{
   "message": "No converter found for return value of type: class java.lang.String",
   "type": "IllegalArgumentError"
}]}

好像杰克逊的串行器正在干扰响应并抛出上述错误。

对此有什么解决方案?

4 个答案:

答案 0 :(得分:0)

Spring使用Jackson将POJO转换为json。默认情况下,Jackson使字段名的第一个字母变小。如果需要自定义名称,请添加以下杰克逊注释。

@JsonProperty("CustomerId")
private String customerId; 

答案 1 :(得分:0)

@Consumes在这种情况下对您没有太大帮助。它应该用于指定输入格式。查看本文: http://www.javainterviewpoint.com/jax-rs-rest-consumes-example/

如果在调试过程中可以看到jsonOutput,则问题可能与客户端期望输出的方式有关:客户端可能期望使用JSON而不是String对象。 解决方案:返回CustomerResponse而不是String。

@RestController
...
public class CustomerController {
  ...
  public CustomerResponse postCustomerRequest(final HttpServletRequest request) 
{

如果要更改默认命名,则CustomerResponse的字段应标有@JsonProperty。

@JsonProperty(name = "Id")
protected int id;

@JsonProperty(name = "CustomerId")
protected String customerId;

答案 2 :(得分:0)

我将使用您的自定义属性命名策略配置 while($reader->read()) { if($reader->nodeType == XMLREADER::ELEMENT) { $name = (string)$reader->name; $reader->read(); $valu = (string)$reader->value; echo $name .' --> '.$valu . "\n"; } } ,在IMHO中,与在每种控制器方法中序列化为json相比,这是更清洁的解决方案

MappingJackson2HttpMessageConverter

或者,如果您确实想要或需要控制响应主体,请确保<mvc:annotation-driven> <mvc:message-converters> ... <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper" ref="objectMapper"/> </bean> ... </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="propertyNamingStrategy" ref="customNamingStrategy" /> </bean 已在spring MVC配置中注册为转换器

StringHttpMessageConverter

答案 3 :(得分:0)

我通过移动到没有杰克逊设置的扩展程序来使其工作。 PS:这是在SAP Hybris和 它包含多个扩展/项目。

我用@Controller标记了Controller,添加了@Produces({MediaType.APPLICATION_JSON}),删除了@ResponseBody,将方法返回类型设置为ResponseEntity, 使用PropertyNamingStrategy转换大写的第一个字母。

public ResponseEntity<String> postCustomerRequest(final HttpServletRequest request) {
    ...
    final org.codehaus.jackson.map.ObjectMapper mapperObj = new org.codehaus.jackson.map.ObjectMapper();
    CustomerResponse response=createCustomer(document,objectFactory);
    mapperObj.setPropertyNamingStrategy(new CustomerJsonNameStrategy());

    final HttpHeaders httpHeaders= new HttpHeaders();
    httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
    return new ResponseEntity<String>(mapperObj.writeValueAsString(response), httpHeaders, HttpStatus.OK);

}