How to Accept JSON input in Spring Restful Webservice?

27,488

Solution 1

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.

Solution 2

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.

Share:
27,488
user2985202
Author by

user2985202

Updated on July 12, 2022

Comments

  • user2985202
    user2985202 almost 2 years

    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.