Some Questions and Answers for my book "Developing Enterprise Java Applications with
Spring Frameworks: An End-to-End Approach"

Q11: Listing 6.2 RestWebAPITest.java returns html rather than JSON. How do I fix it?

This was an issue caused by upgrading SOBA from Spring 3.1 to 3.2GA. In Spring 3.1, accepting headers was
honored by default, but in Spring 3.2, we must explicitly configure the HeaderContentNegotiationStrategy bean as
shown below to enable accepting the header specified by the client. If you downloaded the project file after Oct 25,
2013, this issue has been fixed by introducing the following changes in the soba-servlet.xml file:

                    <bean class="org.springframework.web.accept.ContentNegotiationManager">
                            <constructor-arg>
                                    
<list>
                                            <bean
                                                    class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                                                    <constructor-arg>
                                                            <map>
                                                                    <entry key="json" value="application/json" />
                                                                    <entry key="xml" value="application/xml" />
                                                                    <entry key="html" value="text/html" />
                                                            </map>
                                                    </constructor-arg>
                                            </bean>
                                            
<bean
                                                    class="org.springframework.web.accept.HeaderContentNegotiationStrategy" />
                                    </list>
                            </constructor-arg>
                    </bean>

You can also build a Restful API to find a transaction by ID as follows:

1) Add <mvc:annotation-driven /> right after <context:annotation-config /> in soba-servlet.xml to enable MVC
annotation.
2) Add the following method in RestTxController.java (note the capital D in txID to distinguish it from the previous
html response version)

@Secured("ROLE_CUST")
@RequestMapping(value = "/txID/{transactionId}", method = RequestMethod.GET)
public @ResponseBody BankingTx getTransactionByID(@PathVariable String transactionId) {
BankingTx transaction = bankingTxDao.findByTransactionID(transactionId);
return transaction;
}

Note: you also need to add import org.springframework.web.bind.annotation.ResponseBody; in
RestTxController.java to compile

3) Accordingly, in the RestWebAPITest.java, change "txId" to "txID"
4) Rebuild and deploy SOBA and test the RestWebAPITest.java as described in Chapter 6 again.
Counter

Books are my best friends ...