Answer the question
In order to leave comments, you need to log in
Why did REST forget about JSON?
I actually know the depths of the fiber, and took up this manual: https://spring.io/guides/gs/rest-service/
Actually the problem:
o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms
o.s.web.servlet.DispatcherServlet : GET "/message", parameters={}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to by.alex.webrest.RESTApp#GetMessage(String)
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class by.alex.webrest.RESTMessage]
o.s.web.servlet.DispatcherServlet : Completed 500 INTERNAL_SERVER_ERROR
o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 500
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>by.alex</groupId>
<artifactId>webrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webrest</name>
<description>Some just REST service</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package by.alex.webrest;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@RestController
public class RESTApp {
private static final String template = "Hello, %s";
private final AtomicLong requestCounter = new AtomicLong();
@GetMapping("/message")
@ResponseBody
RESTMessage GetMessage(@RequestParam(name = "name", defaultValue = "my friend!") String target) {
return new RESTMessage(requestCounter.incrementAndGet(), String.format(template, target));
}
}
package by.alex.webrest;
public class RESTMessage {
private final long id;
private final String content;
RESTMessage(long id, String content) {
this.id = id;
this.content = content;
}
long getId() {
return this.id;
}
String getContent() {
return this.content;
}
}
package by.alex.webrest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
@SpringBootApplication
public class WebrestApplication {
public static void main(String[] args) {
SpringApplication.run(WebrestApplication.class, args);
}
}
Answer the question
In order to leave comments, you need to log in
Despite the fact that the example of the spring itself works without additional. annotations to the class/class fields, the solution turned out to be very simple:
@JsonSerialize
public class Tag {
@JsonProperty("id")
private long Id;
@JsonProperty("name")
private String Name;
@JsonProperty("createdAt")
private ZonedDateTime CreatedAt;
@JsonProperty("updatedAt")
private ZonedDateTime UpdatedAt;
....
@RestController
public class RESTAPI {
private static final AtomicLong counter = new AtomicLong();
@GetMapping("/tags")
Tag tags(@RequestParam(name = "name", defaultValue = "anytag") String tagName) {
return new Tag(this.counter.incrementAndGet(), tagName);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question