如何在Spring Restful Webservice中接受JSON输入?

前端之家收集整理的这篇文章主要介绍了如何在Spring Restful Webservice中接受JSON输入?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我很难接受JSON输入到Spring Restful Webservice中.基本上我的目的是接受一个JSON并返回一个zip文件.但我无法跨越自己的第一步.
以下是控制器代码

@Controller
@RequestMapping(value = "/request")
public class PasskitController {

@Autowired
@Qualifier("PassManager")
private PassManager pm;

/*headers = { "Accept:application/json" },consumes = MediaType.APPLICATION_JSON_VALUE,*/

@RequestMapping(value = "/createPass",method = RequestMethod.POST,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody ByteArrayOutputStream createGiftPass(
        @RequestBody PassGenerationRequest request) throws IOException {
    System.out.println("in createGiftPass() method");
    String success = "Success";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(success.getBytes());
    return baos;
}

@RequestMapping(value = "/test",method = RequestMethod.GET,produces = MediaType.TEXT_PLAIN_VALUE)
public @ResponseBody
String test() throws IOException {
    System.out.println("in test() method");
    return "Success";
}
}

我需要将输入JSON映射到以下pojo PassGenerationRequest

@JsonAutoDetect
public class PassGenerationRequest {

private String serialNumber;
private String upc;
private String campaign;
private String merchant;

public String getSerialNumber() {
    return serialNumber;
}

public void setSerialNumber(String serialNumber) {
    this.serialNumber = serialNumber;
}

public String getUpc() {
    return upc;
}

public void setUpc(String upc) {
    this.upc = upc;
}

public String getCampaign() {
    return campaign;
}

public void setCampaign(String campaign) {
    this.campaign = campaign;
}

public String getMerchant() {
    return merchant;
}

public void setMerchant(String merchant) {
    this.merchant = merchant;
}
}

以下是spring-servlet.xml中配置的不同HttpMessageConverters

目前我正在获取内容类型’text / plain; charset = UTF-8’不支持异常.

如果我添加header = {“Accept:application / json”},那么我得到异常,说找不到请求“request / createPass”的处理程序

有人可以帮帮我吗?

谢谢.

最佳答案
@H_502_37@验证您的请求是否已将Content-Type设置为application / json.

Accept描述您要在响应中看到的媒体类型.你有二进制数据集,所以当你提供application / json时,Spring看不到匹配.

原文链接:https://www.f2er.com/spring/432328.html

猜你在找的Spring相关文章