7

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.

2
  • What does your request look like? Commented Nov 15, 2013 at 2:52
  • I was actually using Postman app to post my request. Commented Nov 15, 2013 at 5:47

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I was using Postman which is a chrome app to post my requests. I am setting the Content Type as JSON still I am getting the same issue. I tried posting through CURL as well. Following is my CURL request curl -i -H "Content-Type: application/json" -X POST -d '{"serialNumber":"1234","upc":"12345","campaign":"155950","merchant":"ASDA"}' http://localhost:8080/passkit-service-app-1.0/passkit/request/createPass
Even if I include the header info as headers = { "Accept:application/octet-stream" } which I believ is applicable if I am returning ByteArrayOutputStream it gives me HTTP 404 which is no resource found
What is the context for your web application? For example, maybe it's passkit and the url should be http://localhost:8080/passkit/request/createPass
The url http://localhost:8080/passkit-service-app-1.0/passkit/request/test works in case of test() method in the code above. It does not work for http://localhost:8080/passkit-service-app-1.0/passkit/request/createPass which is the url for createGiftPass() method. Also I tried converting test() method to POST and it started giving 404 error. So it looks like the issue with POST methods.
Is there anything I am missing in the spring-servlet configuration or controller? May be a content negotiating view resolver (not sure if that is applicable in this case). Is there any working example of Spring Rest Webservice with JSON as input which you can point me to.
1

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.

Comments

0

use this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.