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

时间:2013-11-15 02:50:13

标签: java json spring web-services spring-mvc

我很难接受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,
        consumes = MediaType.APPLICATION_JSON_VALUE,
        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

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter" />
            <ref bean="byteArrayMessageConverter"/>
            <ref bean="stringMessageConverter"/>
        </list>
    </property>

</bean>

<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
    <property name="supportedMediaTypes" value="application/json" />
</bean>

<bean id="byteArrayMessageConverter"
    class="org.springframework.http.converter.ByteArrayHttpMessageConverter" >
    <property name="supportedMediaTypes" value="application/octet-stream" />
</bean>

<bean id="stringMessageConverter"
    class="org.springframework.http.converter.StringHttpMessageConverter" >
    <property name="supportedMediaTypes" value="text/plain" />
</bean>

目前我正在获取内容类型'text / plain; charset = UTF-8'不支持例外。

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

有人可以帮助我吗?

感谢。

3 个答案:

答案 0 :(得分:1)

确认您的请求Content-Type设置为application/json

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

答案 1 :(得分:1)

确保Jackson库在类路径中消息转换器MappingJacksonHttpMessageConverter用于rel 2之前的旧jackson lib,在rel 2之后需要添加MappingJackson2HttpMessageConverter, 你也可以用简单的“”删除注释方法处理程序,并在类路径中使用jackson lib,它会自动选择你需要的消息转换器。

答案 2 :(得分:0)

使用此

              

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter" />
        </list>
    </property>
</bean>