我很难接受 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"
任何人都可以在这里帮助我吗?
谢谢。