我在将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”找不到处理程序的异常。
请问有人可以帮我解决这个问题吗?
谢谢。