我正在将 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
最佳答案
关于java - 400 在 Spring MVC 4 Rest 中转换 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26892452/