繁体   English   中英

400在Spring MVC 4 Rest中转换JSON时出错

[英]400 Error converting JSON in Spring MVC 4 Rest

我正在将JSON对象发送到Java Rest服务,并返回400 Bad Request错误。 我正在通过Angular JS服务发送请求。

Java配置:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "gov.mt.dphhs.bts.rest")
public class MvcConfig extends WebMvcConfigurerAdapter {
    private static final Logger logger = LoggerFactory.getLogger(MvcConfig.class);

    @Autowired
    ObjectMapper objectMapper;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        logger.debug("Initializing the Hibernate Aware Jackson Mapper for JSON.");
        super.configureMessageConverters(converters);
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper);
        converters.add(converter);
    }
}

Java REST服务:

 public class AbstractRestController<T, I extends Serializable> {

    private final GenericService<T, I> service;

    public AbstractRestController(GenericService<T, I> service) {
        this.service = service;
    }

    /**
     * Handles request to save entity of type T
     * 
     * @param entity
     *            - the entity to be saved
     * @return the saved entity
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody T save(@RequestBody T entity) {
        return service.save(entity);
    }  

...

@RestController
@RequestMapping(value = "/rest/bill")
public class BillRestController extends AbstractRestController<Bill, Long>

角服务:

(function(){
    var app = angular.module("billDetailsServiceModule", []);
    app.service('billDetailsService', function($log, $http){ 
          this.save = function(bill){ 
              return $http({
                  url: 'api/rest/bill/save', 
                  method: 'POST',
                  data: bill, 
                  headers: {
                        'Content-Type': 'application/json; charset=utf-8',
                        'Accept': 'application/json;odata=verbose'
                    }
              });
          };
})();

从同一个服务调用以获取REST服务中的方法就可以了。 我已经用Fiddler捕获了请求,并且根据JSONLint,JSON对象格式正确。 我正在使用Jackson 2.4.3和Spring 4.1.0。 我已经阅读了许多类似的问题,但没有一种解决方案适合我。

编辑:此处粘贴的日志: http : //chopapp.com/#hpc3axxc

更改此行:

数据:帐单,

对此:

数据:JSON.stringify(bill),

为了捕获这样的错误,我使用Burp HTTP Proxy之类的工具来查看所有原始请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM