Java 如何在 Spring Restful Webservice 中接受 JSON 输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19992623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Accept JSON input in Spring Restful Webservice?
提问by user2985202
I am having hard time accepting JSON input into my Spring Restful Webservice. Basically my purpose is to accept a JSON and return a zip file. But I am not being able to cross first step itself. Following is the controller code
我很难接受 JSON 输入到我的 Spring Restful Web 服务中。基本上我的目的是接受一个 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";
}
}
I need to map the input JSON into following pojo PassGenerationRequest
我需要将输入 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;
}
}
Following are the different HttpMessageConverters configured in spring-servlet.xml
以下是 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.MappingHymansonHttpMessageConverter" >
<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>
Currently I am getting Content type 'text/plain; charset=UTF-8' not supportedexception.
目前我正在获取内容类型“文本/纯文本;charset=UTF-8' 不支持异常。
If I add the header={"Accept: application/json"}then I get exception saying No handler found for the request "request/createPass"
如果我添加header={"Accept: application/json"}然后我得到异常说没有找到请求“request/createPass”的处理程序
Can anyone please help me out over here?
任何人都可以在这里帮助我吗?
Thanks.
谢谢。
回答by Vidya
Verify your request has Content-Type
set to application/json
.
验证您的请求已Content-Type
设置为application/json
。
Accept
describes the media type you want to see in the response. You have that set for binary data, so when you provide application/json
instead, Spring doesn't see a match.
Accept
描述您希望在响应中看到的媒体类型。您已经为二进制数据设置了该设置,因此当您提供时application/json
,Spring 看不到匹配项。
回答by HKumar
Make sure Hymanson library are in class path Message converter MappingHymansonHttpMessageConverter is for old Hymanson lib before rel 2 , after rel 2 you need to add MappingHymanson2HttpMessageConverter, Also u can remove annotation method handler with simple "" and with Hymanson lib in class path, it will automatically pick your required message converter.
确保 Hymanson 库在类路径中 消息转换器 MappingHymansonHttpMessageConverter 适用于 rel 2 之前的旧 Hymanson lib,在 rel 2 之后您需要添加 MappingHymanson2HttpMessageConverter,您也可以使用简单的“”和类路径中的 Hymanson lib 删除注释方法处理程序,它将自动选择您需要的消息转换器。
回答by Ghanshyam Saini
use this
用这个
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>