7

我很难接受 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"}然后我得到异常说No handler found for the request "request/createPass"

任何人都可以在这里帮助我吗?

谢谢。

4

3 回答 3

1

验证您的请求已Content-Type设置为application/json

Accept描述您希望在响应中看到的媒体类型。您已经为二进制数据设置了该设置,因此当您提供时application/json,Spring 看不到匹配项。

于 2013-11-15T02:59:03.410 回答
1

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

于 2014-04-11T07:33:06.043 回答
0

用这个

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter" />
        </list>
    </property>
</bean> 
于 2014-11-07T09:15:06.107 回答