user2985202
user2985202

Reputation: 71

How to Accept JSON input in Spring Restful Webservice?

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

@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

@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

<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>

Currently I am getting Content type 'text/plain; charset=UTF-8' not supported exception.

If I add the header={"Accept: application/json"} then I get exception saying No handler found for the request "request/createPass"

Can anyone please help me out over here?

Thanks.

Upvotes: 7

Views: 29925

Answers (3)

Ghanshyam Saini
Ghanshyam Saini

Reputation: 1

use this

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

Upvotes: 0

HKumar
HKumar

Reputation: 655

Make sure Jackson library are in class path Message converter MappingJacksonHttpMessageConverter is for old jackson lib before rel 2 , after rel 2 you need to add MappingJackson2HttpMessageConverter, Also u can remove annotation method handler with simple "" and with jackson lib in class path, it will automatically pick your required message converter.

Upvotes: 1

Vidya
Vidya

Reputation: 30310

Verify your request has Content-Type set to 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.

Upvotes: 1

Related Questions