Converting JSON data in Spring MVC 3.2 for REST services

I am encountering an issue while attempting to send a List of data as JSON from my Spring Controller. The error message "Could not find acceptable representation" is being thrown. Below are the snippets of code from different parts of my application:

pom.xml

<hibernate.version>4.1.4.Final</hibernate.version>
<spring.version>3.2.3.RELEASE</spring.version>
<spring.data.version>1.3.2.RELEASE</spring.data.version>
<jackson.version>1.9.12</jackson.version>
<spring.security.version>3.2.2.RELEASE</spring.security.version>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>${jackson.version}</version>
</dependency>

<!-- 
<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.1.0</version>
</dependency>
 -->

WebConfig. I have explored various blogs and stackoverflow posts for solutions, including the commented ones.

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.test.testing")
//@PropertySource("classpath:application.properties")
@EnableJpaRepositories("com.test.testing.config.dao")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Resource
    private Environment env;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }


    ...

Controller. Output from the controller method:

getMasterTypeData Master Data Type List :[MasterType [id=1, value=UAT/QA], MasterType [id=4, value=Development], MasterType [id=2, value=Model], MasterType [id=3, value=Production]]

@RequestMapping(value = "/getdata/{type}", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<MasterType> getMasterTypeData(@PathVariable String type) {

        MASTERDATATYPE master = MASTERDATATYPE.getMasterDataType(type); 
        List<MasterType> masterdatTypeList = new ArrayList<MasterType>();
        System.out.println("getMasterTypeData Input :"+type+",MASTERDATATYPE :"+master);

        // some logics to populate masterdatTypeList 

        System.out.println("getMasterTypeData Master Data Type List :"+masterdatTypeList);
        return masterdatTypeList;
    }

and finally the MasterType class that should be sent back to the browser as a JSON object:

@JsonAutoDetect
public class MasterType implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Long id;
...

Could you please assist me with resolving this issue?

Answer №1

Ensure that your request headers include "Accept application/json". Use tools like Firebug to examine the request headers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choosing a datepicker in Selenium can be easily done by identifying the date

Hello, I need help selecting a datepicker from an image. I am trying to select the date 27th March 2016. https://i.stack.imgur.com/OW80A.png Here is the code for the datepicker: <div id="widget_dijit_form_DateTextBox_6" class="dijit dijitReset dijitI ...

Converting JSON Responses in Android: A Guide

After creating my JSON response model class, here is how it looks: data class MyModelClass( val one: String, val two: String, val three: String, val four: Int, val tempCurrentCharge: List<CurrentCharge>, ...

Convert JSON information into an array and map it in Swift

I am currently facing a certain challenge. My approach involves using Alamofire request to retrieve a JSON response. Alamofire.request(url, method: .get).responseJSON { response in if response.result.isSuccess { print ...

Using AJAX to send a request with an image to a Spring MVC controller

I am attempting to utilize AJAX to send an image: function uploadImage() { var image = document.getElementById('image').files[0]; var formData = new FormData(); formData.append('image', image); $.ajax({ url: &ap ...

Generating hierarchical structures from div elements

Looking for guidance on how to parse a HTML page like the one below and create a hierarchical Javascript object or JSON. Any assistance would be much appreciated. <div class="t"> <div> <div class="c"> <input t ...

Converting NSMutableDictionary to JSON results in unexpected formatting issues on iOS

One of the challenges I'm facing is converting a NSMutableDictionary to JSON on iOS. NSMutableDictionary *offers = [[NSMutableDictionary alloc] init ]; for(int i=0;i<2;i++) { NSMutableDictionary *myOffer = [[NSMutableDictionary alloc ...

Using PHP to combine MySQL tables and then displaying the results in a neatly organized JSON

Struggling to merge data from two MySQL tables in my PHP script and transform it into JSON for use in our internal art team app. This tool is exclusively designed for making edits to products created by our company. The main goal is to generate a product ...

What is the best way to transmit a JSON object to REST services using Angular?

Whenever I attempt to send the JSON object to REST services, I encounter an error that looks like this: http://localhost:8080/api/v1/cardLimit 400 (Bad Request); JSON Object Example: public class GameLimit implements Serializable { private stati ...

Uploading JSON file Size into Snowflake Variant Column

Our Snowflake table contains JSON files loaded into a Variant column. The table consists of two columns - File name and Variant column (JSON records). While I can determine the total size of the table using information schema, I am now looking to calculat ...

Encountering a problem when trying to find an element with xpath in Selenium 3

I am encountering an issue while attempting to click on a link that resembles a tab button using Xpath. Here is the snippet of html: <div id="tile_8" style="height: 93px; width: 26%; background-color: rgb(45, 87, 19); color: white; position: relativ ...

Selenium reports an error with the xpath used for the span element,

HTML : <div class="accept"><a class="red_keep popup_action"><span>Tap here to see the terms</span></a></div> Xpath attempts : 1. //span[text()='Tap here to view terms'] 2. normalize-space(//href[@class="red_ ...

No default export found in Module: TypeScript RestAPI

The word controller is showing a red underline when I use import controller from '../controllers/test.controller'; In my typescript rest api application, the structure looks like this... project structure I am puzzled by the red line under cont ...

Is the issue with AJAX and a global variable a result of my misunderstanding?

My goal is to use AJAX to load client data onto a page and then replace a company ID with the corresponding name from a different company table in the same database. However, I am facing an issue where the global JavaScript variable is not being updated wi ...

A concern arises with security when utilizing jQuery alongside json for requesting data from a service

When calling a service with jQuery, there may be concerns about exposing the service call to savvy users who could intercept it using tools like Fiddler or custom web applications. $.ajax({ url: SERVICE_URL?Q1=eee&Q2=sss, dataType: "application/ ...

Is there a way to bypass strict JSON parsing when using jQuery's .ajax() function?

From jQuery 1.9 onwards, when using dataType: json in a .ajax call and the response body cannot be parsed as JSON, the request will fail silently: The parsing of JSON data is strict; any malformed JSON will be rejected with a parse error. Starting from ...

Retrieve JSON information from a database using an API to showcase additional outcomes

I am currently developing an iOS application that needs to fetch and display data from a MYSQL database. I have set up an API to establish communication between the app and the database. The database is sending valid JSON, as shown below: [{"name":"Mauric ...

Accessing and manipulating a intricate JSON structure within an Ionic 3 framework to seamlessly connect it to the user

I'm currently developing an app using Ionic 3. Within my project, I have a JSON object structured like this: { "player": { "username": "thelegend", "platform": "xbox", "stats": { "normal": { "shots ...

The spring controller sends back HTML content

Currently, I am working on a project involving Spring MVC, Liferay, and jQuery. One issue that I am facing is related to an ajax request (post) which calls a controller method annotated with @ActionMapping. When the ajax callback function is triggered, th ...

Trouble navigating to the 'current active tab' in Chrome with WebDriver using Java

I am facing difficulty switching to the 'current active tab' in Chrome using WebDriver. Despite trying multiple approaches, I have been unsuccessful so far. Navigating to a different tab is possible, but determining which one is the current act ...

Having trouble locating element using xpath within a div containing ::before pseudo-element

When working with the web driver object, I often need to obtain a list of web elements. findElements(By.xpath("")); Usually, I use xpath like //*[@class=\"providers-list clearfix\"] to retrieve the list. However, I encounter an error when tryin ...