Guida - cos'altro posso fare per correggere questo errore? org.springframework.web.HttpMediaTypeNotAcceptableException :: Impossibile trovare una rappresentazione accettabile.HttpMediaTypeNotAcceptableException: Impossibile trovare la rappresentazione accettabile mvc 4.1.5 con com.fasterxml 2.5.1
Penso che il mio progetto sia configurato correttamente per gestire le richieste restanti di JSON. Negli ultimi giorni, ho letto e applicato vari suggerimenti inutilmente. Ci sono altre idee su che cosa dovrei fare diversamente?
Sto utilizzando Spring MVC 4.1.5 e com.fasterxml 2.5.1.
Una parte del mio pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
<type>bundle</type>
</dependency>
Ecco parte della mia web.xml mia
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Ecco parte della mia servlet-context.xml
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.losgatos">
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
Ecco il mio controller
package com.losgatos.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.losgatos.User;
@RestController
@RequestMapping("user")
public class UserController {
@RequestMapping(value = "data", produces="application/json")
public User getUser(){
return new User();
}
}
Ecco il POJO utente Utente
package com.losgatos;
import java.util.EnumSet;
public class User {
public User(){
id = 0;
age = 22;
name = "Titus Feng";
alias = "tornado tie";
roles = EnumSet.of(Role.NORMAL, Role.NEW);
}
private int id, age;
private String name, alias;
private EnumSet<Role> roles;
//added getters and setters here
public enum Role{
NEW, NORMAL, ADMIN, MEMBER, DORMANT
}
}
puoi mostrarci la configurazione AJAX come si chiama il servizio REST? – Rembo
Al momento, sto chiamando http: // localhost: 8080/user/data direttamente dal browser solo per verificare che funzioni come previsto – 000
Vedere anche http://stackoverflow.com/questions/28466207/could-not- find-accept-representation-using-spring-boot-starter-web –